应用程序被杀死/清除时未收到FCM推送通知 [英] FCM push notification not received when app is killed/swiped out
本文介绍了应用程序被杀死/清除时未收到FCM推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将通知从GCM迁移到FCM.在GCM中,当应用程序处于前景,背景或被杀死时,我能够收到通知,但是在FCM中,当应用程序未被刷出打开/杀死时,我无法接收通知.我有什么遗漏吗???
I am migrating notification to FCM from GCM. In GCM, I was able to receive notification when app was in foreground, background or killed, but in FCM, I am not able to receive notification when app is not opened/killed by swipe out. Am I missing anything????
AndroidManifest.xml
AndroidManifest.xml
<service
android:name=".Services.ExtendedInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Services.FcmMessageHandler"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
FCMMessageHandler.java
FCMMessageHandler.java
公共类FcmMessageHandler扩展了FirebaseMessagingService {
public class FcmMessageHandler extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
// Check if message contains a data payload.
if (message.getData().size() > 0) {
Log.d("ABC", "Message data payload: " + message.getData());
}
sendNotification("test");
}
private void sendNotification(String message) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(getResources().getColor(R.color.button_background))
//.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setContentTitle("App")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationNumber /* ID of notification */, notification);
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.ic_notification : R.drawable.ic_launcher;
}
}
json请求正文:
{
"notification":{
"title":"Title",
"message":"Message.",
"icon" = "ic_notification",
"sound" = "default",
"click_action":"FCM_PLUGIN_ACTIVITY"
},
"data":{
"title":"Title",
"message":"Message.",
"icon" = "ic_notification",
"sound" = "default"
},
"to":"chIFk6ljlfdskfdsifosdifnodsifnsdofindsofindsfoindsifvndsnmdkslakjdn2p",
"priority":"high"
}
推荐答案
我让您使用按摩标签巫婆r替换为身体标签.更改服务器中的有效负载.
i got you r using massage tag witch r replaced with body tag. change payload in server.
{
"to" : "deviceToken",
"notification" : {
"body" : "Pass body here",
"title" : "Title For Notification",
"icon" : " icon ",
"sound" : "notification sound here"
}
}
这篇关于应用程序被杀死/清除时未收到FCM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文