在fcm中单击通知时打开特定活动

a14dhokn  于 2021-07-09  发布在  Java
关注(0)|答案(5)|浏览(423)

我正在开发一个需要显示通知的应用程序。对于通知,我使用firebase云消息传递(fcm)。我可以得到通知时,应用程序是在后台。
但当我单击notification时,它会重定向到home.java页面。我希望它重定向到notification.java页面。
所以,请告诉我如何在点击通知中指定活动。我使用两种服务:
1.)myfirebasemessagingservice
2.)MyFireBaseInstancedService
这是myfirebasemessagingservice类中onmessagereceived()方法的代码示例。

  1. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  2. private static final String TAG = "FirebaseMessageService";
  3. Bitmap bitmap;
  4. public void onMessageReceived(RemoteMessage remoteMessage) {
  5. Log.d(TAG, "From: " + remoteMessage.getFrom());
  6. // Check if message contains a data payload.
  7. if (remoteMessage.getData().size() > 0) {
  8. Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  9. }
  10. // Check if message contains a notification payload.
  11. if (remoteMessage.getNotification() != null) {
  12. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  13. }
  14. // Also if you intend on generating your own notifications as a result of a received FCM
  15. // message, here is where that should be initiated. See sendNotification method below.
  16. }
  17. /**
  18. * Create and show a simple notification containing the received FCM message.
  19. */
  20. private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
  21. Intent intent = new Intent(this, Notification.class);
  22. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  23. intent.putExtra("Notification", TrueOrFalse);
  24. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  25. PendingIntent.FLAG_ONE_SHOT);
  26. Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  27. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  28. .setLargeIcon(image)/*Notification icon image*/
  29. .setContentTitle(messageBody)
  30. .setStyle(new NotificationCompat.BigPictureStyle()
  31. .bigPicture(image))/*Notification with Image*/
  32. .setAutoCancel(true)
  33. .setSound(defaultSoundUri)
  34. .setContentIntent(pendingIntent);
  35. NotificationManager notificationManager =
  36. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  37. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  38. }
  39. /*
  40. * To get a Bitmap image from the URL received
  41. * */
  42. public Bitmap getBitmapfromUrl(String imageUrl) {
  43. try {
  44. URL url = new URL(imageUrl);
  45. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  46. connection.setDoInput(true);
  47. connection.connect();
  48. InputStream input = connection.getInputStream();
  49. Bitmap bitmap = BitmapFactory.decodeStream(input);
  50. return bitmap;
  51. } catch (Exception e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. return null;
  55. }
  56. }
kqhtkvqz

kqhtkvqz1#

下面是理解这一点的最简单方法。
当您发送数据负载时,通知负载为

  1. notification: {
  2. title: "Your order status.",
  3. body: orderStatusDetail,
  4. clickAction: "ShopFragment"
  5. },
  6. data: {
  7. ORDER_ID: orderId
  8. }

通知clickaction将是用于将数据传递到活动的筛选器,什么数据?服务器发送的数据 data: { } 附加到有效负载的对象。
因此,clickaction将触发清单中的意图过滤器,因此首先我们需要创建它

  1. <activity
  2. android:name=".activity.MainActivity"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="ShopFragment"/>
  6. <category android:name="android.intent.category.DEFAULT"/>
  7. </intent-filter>
  8. </activity>

现在,我们使用与clickaction相同的名称设置意图过滤器,这样做会触发每当我们按下通知选项卡上的通知时,该意图过滤器将启动,依此类推与该意图过滤器关联的活动。
然后,使用 intent.getStringExtra("ORDER_ID") 在main活动中,在数据有效负载中发送额外的字符串。
在这种情况下,请确保订单号是我们从 data { } 对象,并且需要在客户端中相同才能获取此数据。

展开查看全部
4xrmg8kj

4xrmg8kj2#

打开myfirebasemessagingservice.java文件
在该文件中有一个sendnotification()方法,您必须在该方法中指定需要在intent中导航到的活动,如下所示

  1. Intent intent = new Intent(this, YourActivityName.class);

如果您正在发送多个通知,并且希望在单击某个特定通知时导航到不同的活动,您可以使用任何条件语句来实现它,我的建议是使用如下所示的switch case

  1. private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
  2. Intent intent = new Intent();
  3. switch(condition) {
  4. case '1': intent = new Intent(this, ActivityOne.class);
  5. break;
  6. case '2': intent = new Intent(this, ActivityTwo.class);
  7. break;
  8. case '3': intent = new Intent(this, ActivityThree.class);
  9. break;
  10. default : intent = new Intent(this, DefaultActivity.class);
  11. break;
  12. }
  13. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  14. intent.putExtra("Notification", TrueOrFalse);
  15. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  16. PendingIntent.FLAG_ONE_SHOT);
  17. }

使用此逻辑,您可以在fcm中单击通知时打开特定活动。这对我很有效。谢谢

展开查看全部
wydwbb8l

wydwbb8l3#

androidmanifest.xml文件

  1. <activity android:name="YOUR_ACTIVITY">
  2. <intent-filter>
  3. <action android:name="com.example.yourapplication_YOUR_NOTIFICATION_NAME" />
  4. <category android:name="android.intent.category.DEFAULT" />
  5. </intent-filter>
  6. </activity>

您的firebasemessagingservice.java文件onmessagereceived方法:

  1. public void onMessageReceived(RemoteMessage remoteMessage){
  2. String title=remoteMessage.getNotification().getTitle();
  3. String message=remoteMessage.getNotification().getBody();
  4. String click_action=remoteMessage.getNotification().getClickAction();
  5. Intent intent=new Intent(click_action);
  6. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  7. PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
  8. NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
  9. notificationBuilder.setContentTitle(title);
  10. notificationBuilder.setContentText(message);
  11. notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
  12. notificationBuilder.setAutoCancel(true);
  13. notificationBuilder.setContentIntent(pendingIntent);
  14. NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  15. notificationManager.notify(0,notificationBuilder.build());
  16. }

用于通知的云函数/服务器代码必须如下所示:

  1. notification: {
  2. title: "TITLE OF NOTIFICATION",
  3. body: "NOTIFICATION MESSAGE",
  4. sound: "default",
  5. click_action:"com.example.yourapplication_YOUR_NOTIFICATION_NAME"
  6. }
展开查看全部
s71maibg

s71maibg4#

当应用程序在后台时,必须在启动程序活动中传递意图。所以它打开了你的启动活动。现在检查启动程序活动的intent中是否有数据,然后启动所需的活动。
在你的发射器活动里面

  1. Bundle extras = getIntent().getExtras();
  2. if (extras != null) {
  3. // possible launched from notification
  4. // check if desired notification data present in extras then its
  5. // confirmed that launched from notification
  6. }else{
  7. // not launched from notification
  8. }
6qqygrtg

6qqygrtg5#

使用fcm,您可以向客户端发送两种类型的消息:
1.通知消息:有时被认为是“显示消息”
fcm代表客户端应用程序自动向最终用户设备显示消息。通知消息具有一组预定义的用户可见键。
2.数据消息:由客户端应用程序处理。
客户端应用程序负责处理数据消息。数据消息只有自定义的键值对。
根据fcm文档,在android应用程序中接收消息
应用程序处于后台时发送的通知。在这种情况下,通知被传递到设备的系统托盘。默认情况下,用户点击通知会打开应用程序启动程序。
具有通知和数据负载的消息,包括后台和前台。在这种情况下,通知被传递到
设备的系统托盘,并且数据有效负载在您的启动器活动的目的的额外部分中交付。
click_action 在通知负载中:
因此,如果您想处理在后台收到的消息,您必须发送 click_action 带着信息。 click_action 是通知负载的参数
如果要打开应用程序并执行特定操作,请设置 click_action 并将其Map到要启动的活动中的意图过滤器。
例如,设置 click_actionOPEN_ACTIVITY_1 触发如下意图过滤器:

  1. <intent-filter>
  2. <action android:name="OPEN_ACTIVITY_1" />
  3. <category android:name="android.intent.category.DEFAULT" />
  4. </intent-filter>

fcm有效载荷如下所示:

  1. {
  2. "to":"some_device_token",
  3. "content_available": true,
  4. "notification": {
  5. "title": "hello",
  6. "body": "test message",
  7. "click_action": "OPEN_ACTIVITY_1"
  8. },
  9. "data": {
  10. "extra":"juice"
  11. }
  12. }
展开查看全部

相关问题