Android Firebase:当应用程序位于后台时,系统中不显示华为p8推送通知?
根据: 当应用程序位于前台时:Android Firebase:当应用程序位于后台时,系统中不显示华为p8推送通知?,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,根据: 当应用程序位于前台时: public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body")); } private void sendNotification(
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.roshetta_logo_home)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
调用onMessageReceived,我可以显示推送通知并对数据执行任何处理(在这种情况下我没有问题)
应用程序处于后台时:
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.roshetta_logo_home)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
通知系统托盘将显示通知,一旦用户单击通知,将调用启动器活动,通知数据将在包中传递给启动器活动
我的问题是,当应用程序在后台时,通知系统不会显示通知
我没有使用Firebase控制台,因为我无法向通知负载添加数据。所以我使用了RESTful客户端API,但它仍然是相同的问题
请求:
URL:https://fcm.googleapis.com/fcm/send
Method type: POST
Content-Type: application/json
Authorization : key=AAAAFi0N5KM....
Body:
{
"priority" : "high",
"notification" : {
"body" : "This week’s edition is now available.",
"title" : "NewsMagazine.com",
"icon" : "new",
"click_action": "OPEN_ACTIVITY_1" ,
"sound" : "mySound"
},
"data": {
"title": "title " ,
"body": "any vaslue "
},
"registration_ids": ["fVNdKJsWtNQ:APA91bEJ...."]
}
答复:
{
"multicast_id": 6105324374411246344,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1486655756341577%dc81f05ff9fd7ecd"
}
]
}
这种技术在应用程序位于前台时有效,但在后台时不起作用
Android代码:
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.roshetta_logo_home)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
清单
<service android:name=".Util.PushNotification.MyFirebaseMessagingService"
>
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".Util.PushNotification.MyFirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
尝试使用
to:
而不是
registration_ids
文件建议我们仅在有多个最近者时才注册ID
注册号
字符串数组此参数指定接收多播消息的设备(注册令牌或ID)列表。它必须包含至少1个且最多1000个注册令牌。此参数仅用于多播消息传递,而不用于单个收件人。仅允许使用HTTP JSON格式发送多播消息(发送到多个注册令牌) 同样如您所述,当设备位于后台且有效负载内有通知标记时,不会调用onMessageReceived 系统应该能够自己处理它。确保图标和声音正确保存在适当的文件夹中 如果一切正常,检查设备是否处于睡眠模式(api>=23)。高优先级消息应该能将设备从睡梦中唤醒,但如果设备制造商篡改了安卓操作系统或对其进行了调整,这可能无法工作 如果通知仍未送达,则可能是特定于设备的错误。检查另一台设备以确认 如果所有其他操作都失败,请执行以下操作: 我还建议只发送数据负载,而不是同时发送通知和数据。如果你的目标仅仅是安卓,它会让你在处理什么类型以及何时显示通知方面有更多的控制权
无论是背景还是前景,数据有效负载都将始终有效。它总是在收到消息时触发。每次我们在华为手机上出现问题,都是因为该应用程序不是“受保护的应用程序”
在您的清单文件中尝试此操作。希望您在不修改系统设置的情况下收到推送通知
<service android:name="com.jibarath.MyFirebaseMessagingServices"
android:exported="true"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"></action>
</intent-filter>
</service>
stopwithtask=false成功了
快乐编码 您在什么设备上测试?OnMessageReceived不会在后台调用,因为您的负载有一个通知标记。默认的android系统应该自己处理通知。可能是图标或声音的问题。。。试试看,代码看起来没问题。你有没有在不同的设备上试用过,看看是否是同一种情况?@AL。我在三星和华为试用过,但在华为却不起作用。其他应用程序如inshort和dailyhunt在没有设置更改的情况下是如何运行的?@syedjibharat你找到答案了吗?