【问题标题】:Tap on Notification Bar not getting Message in an Activity if app is in background如果应用程序在后台,点击通知栏没有在活动中收到消息
【发布时间】:2018-01-09 04:50:12
【问题描述】:

我正在按照this 教程在我的应用中实现 Firebase 推送通知 功能。

但我发现了一件事,如果应用程序位于 foreground 中,那么只有我在 Toast 和 TextView 中获取(显示)消息

另一方面,如果应用程序在 background 中点击,我也不会没有收到消息显示在 TextView 和 Toast 中。

而我想在 Toast 和 TextView 两种情况下都显示消息 (Either App is in Foreground or Background)

注意:我正在从 Firebase 控制台本身推送消息。

有可能吗?

MyFirebaseMessagingService.java

private void handleNotification(String message) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }else{
            // If the app is in background, firebase itself handles the notification
        }
    }

MainActivity.java

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // checking for type intent filter
                if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                    // gcm successfully registered
                    // now subscribe to `global` topic to receive app wide notifications
                    FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);

                    displayFirebaseRegId();

                } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                    // new push notification is received

                    String message = intent.getStringExtra("message");

                    Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();

                    txtMessage.setText(message);
                }
            }
        };

【问题讨论】:

  • 如果您想在后台和前台处理通知,那么您可能需要查看此答案Here 并使用邮递员将通知发布到应用程序,您可能还需要查看此答案@ 987654323@

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


【解决方案1】:

FCM 有两种类型的消息,通知和数据。当您希望 FCM 代表您的客户端应用处理显示通知时,请使用通知消息。当您想在客户端应用程序上处理消息时,请使用数据消息。

下面是示例,

 {
  "to": “token ID”,
  "notification": {
     //params
   },
  "data": {
    //params
   }
}

payload 带有消息类型时的行为,

通知消息

  1. 前台 - onMessageReceived 已触发
  2. 背景 - 通知显示在系统托盘上并由 FCM 处理
  3. 应用未运行 - 通知显示在系统托盘上并由 FCM 处理

数据消息

  1. 前台 - onMessageReceived
  2. 背景 - onMessageReceived
  3. 应用未运行 - onMessageReceived

通知和数据

  1. 前台 - onMessageReceived
  2. 背景 - 托盘中的通知和数据负载将通过点击 Intent 的额外内容进行处理
  3. 应用未运行 - 托盘中的通知和数据负载将通过点击时的附加意图来处理。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多