我遵循官方样本并扩展了firebasemessagingservice
<service android:name=".service.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
和我的推JSON
{
"message":{
"token":"my fcm token",
"android":{
"priority": "high",
"data":{
"key1":"123","key2":"456"
}
}
}
}
我从
中重新播放了FCM @Override
public void onMessageReceived(RemoteMessage remoteMessage) {}
直到我从最近的应用中杀死了我的应用程序。
我的Android OS是Android 8.1(API级27)
我在Android 7.1(API级25)上进行了测试,它可以接收消息,甚至应用程序也被杀死。
请参阅此部分:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
System.out.println("Remote Data" + remoteMessage);
//Check if msg contains Data'
if (remoteMessage.getData().size() > 0) {
System.out.println("Message Data : " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("body"));
}
//Check if message contains notification
if (remoteMessage.getNotification() != null) {
System.out.println("Message Body " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
参考链接:
在此处检查
oreo中的deat @youteng通知必须需要通知构建器对象的通道ID。因此,请检查您的代码是否添加了频道ID。您可以查看以下代码。也可以在这里看
// Sets an ID for the notification, so it can be updated.
int notifyID = 1;
String CHANNEL_ID = "my_channel";// channel id
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
实现FCM时有很多细节。使用工具检查发件人侧和接收器侧。
是背景/前景部分。如果您的消息格式不正确,则当应用在后台时,它不会触发OnReceive()。由correct
我的意思是它仅包含data{...}
,没有notification{...}
在挖掘服务器端代码之前,您是否通过从Firebase Console
的Web工具或FCM API
发送消息来测试客户端?
发布https://fcm.googleapis.com/fcm/send
我不知道您在服务器端使用了哪种语言,但是原始消息应该像这样(来自官方)
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
}