这是我的清单:
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
当应用程序在后台并收到通知时,默认通知会出现并且不会运行我的代码 onMessageReceived
。
这是我的 onMessageReceived
代码。如果我的应用程序在前台运行,而不是在后台运行,则会调用此方法。当应用程序也在后台时,如何运行此代码?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// 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.
data = remoteMessage.getData();
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String imageUrl = (String) data.get("image");
String action = (String) data.get("action");
Log.i(TAG, "onMessageReceived: title : "+title);
Log.i(TAG, "onMessageReceived: message : "+message);
Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
Log.i(TAG, "onMessageReceived: action : "+action);
if (imageUrl == null) {
sendNotification(title,message,action);
} else {
new BigPictureNotification(this,title,message,imageUrl,action);
}
}
// [END receive_message]
原文由 Parth Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议
1. 为什么会这样?
FCM(Firebase Cloud Messaging)中有两种类型的消息:
onMessageReceived()
回调onMessageReceived()
回调 ,即使 您的应用程序处于 前台/后台/ 已终止2. 怎么做?
为此,您必须向以下 URL 执行
POST
请求:标头
Content-Type
, 值:application/json
Authorization
, 值:key=<your-server-key>
正文使用主题
或者,如果您想将其发送到特定设备
3. 推送通知消息如何处理?
这是您处理收到的消息的方式: