如何在后台控制 FCM 消息通知

How to get control of FCM message notification in background

我已经在我的应用程序中实施了 FCM Firebase 云消息传递。 当应用程序处于前台模式时,我收到了通知及其数据。

问题:

当我的应用程序处于后台模式或被终止时,通知到达但 FCM 自动生成。所以我无法控制到达的通知数据(通知的自定义数据字段)。 MyFirebaseMessagingService 的 onMessageReceived(RemoteMessage remoteMessage) 方法 class 当应用处于后台模式并且通知到达时不调用。

请帮助获得两种模式下的通知控制(与 GCM 相同)

Problem:

When my app is in background mode or killed at that time notification arrive but its genrate auto by FCM. So I have no control of arrived notification data(coustom data field of notification). onMessageReceived(RemoteMessage remoteMessage) method of MyFirebaseMessagingService class not call when app in background mode and notification arrive.

Please help to get notification control in both mode (Same like GCM)

如果您使用 Firebase 控制台发送通知,则无法控制通知。如果应用程序处于后台或被终止,则不会在 onMessageReceived() 方法中传递从 Firebase 控制台发送的通知。

解决方案:

使用 curl 请求发送通知,不发送通知负载而是发送数据负载。

HTTP POST 请求

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

以上通知将在 onMessageReceived() 方法中传递,无论您的应用程序是在后台还是前台还是被杀死。

尝试使用高级 REST 客户端,图片以获取帮助: