首页 文章

在Android上处理Firebase主题通知

提问于
浏览
1

我正在尝试通过FCM服务发送推送通知 . 在我的MainActivity中,我编写了这段代码:

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();

                }
            }
        };

为了订阅用户主题 .

从后端我通过以下链接拨打FCM服务电话:https://fcm.googleapis.com/fcm/send

我传递这个JSON:

{  
   "to":"/topics/global",
   "data":{  
      "title":"test",
      "is_background":false,
      "message":"testmessage",
      "image":"",
      "payload":{  
         "team":"Test",
         "score":"5.6"
      },
      "timestamp":"2017-05-23 11:55:35"
   }
}

我收到了这个回复:

{\"message_id\":8863205901902209389}

但是,如果我将Firebase控制台与“用户段”或“单个设备”一起使用,则我的设备不会显示任何通知 . 另外在Firebase控制台中不起作用“主题”的方式 .

提前感谢您的回复 .

1 回答

  • 0

    有两种类型的FCM消息 .

    • 通知消息

    • 数据消息

    在客户端,通知消息由FCM处理并自动显示在通知窗口中 . 如果您使用的是数据消息,则您的应用需要处理收到的消息并创建通知 .

    您问题中的样本有效负载是数据消息,因此它不会显示在通知中(我假设您没有进行任何处理) . 通过FCM控制台发送的通知始终是通知消息,因此会自动显示 .

    有关详细信息,请参阅this FCM page .

相关问题