【问题标题】:Android Firebase cloud messaging handling multiple notification [duplicate]Android Firebase云消息处理多个通知[重复]
【发布时间】:2016-08-06 09:26:16
【问题描述】:

我正在使用新的 Firebase 云消息传递并从服务器发送消息,包括通知和数据。我对此有以下疑问:

  1. 应用程序在后台时不会调用 onMessageReceived 并且 通知已创建,因此我如何检测该通知并获取 它的数据无需等待用户点击并从意图中获取数据。

  2. 我在通知中设置了“click_action”以在通知调用时启动 Activity,但即使它已经在运行它也会重新创建 Activity 如何避免这种情况?

【问题讨论】:

  • 对我来说同样的问题。我无法在不等待用户点击的情况下获取通知数据(如果活动已打开)。
  • 如果应用在后台并收到通知消息,则该消息不会传递到应用。相反,Firebase 在通知托盘中显示一条消息,单击该消息会启动应用程序。见stackoverflow.com/questions/37809885/…

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

您提到过,您可以使用意图标志来解决第二个问题。有关可用标志,请参阅此链接:https://developer.android.com/reference/android/content/Intent.html

然后,您可以像这样使用这些标志,

Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

开始活动时。

对于您列出的第三个问题,您可以将待处理的意图分配给单击后将被触发的通知。

Intent intent = new Intent(this, FNotiReceiver.class);
intent.setAction("com.nepal.application.pendingIntent");
intent.putExtra("Name", "Data/Value");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notification_ID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

点击后要删除通知,您可以使用:

NotificationManager notifManager= (NotificationManager)  context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancel(notification_ID);

抱歉,我没有您的第一个问题的确切答案,但我认为创建后台服务会有所帮助。

【讨论】:

    【解决方案2】:

    Firebase 通知处理者,

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
                Log.d(TAG, "From: " + remoteMessage.getFrom());
    
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }
    
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
    
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    

    更多详情 https://firebase.google.com/docs/notifications/android/console-device

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 2016-11-21
      • 2020-06-19
      • 1970-01-01
      相关资源
      最近更新 更多