作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当 App 处于前台时:
onMessageReceived
被调用,我可以显示推送通知并对数据进行任何处理(在这种情况下我没问题)。
当 App 在后台时:
通知系统托盘将显示通知,一旦用户点击通知,启动器 Activity 就会被调用,通知数据将通过 bundle 传递给启动器 Activity 。
我的问题是,当应用程序处于后台时,通知系统不显示通知。
我没有使用 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"
}
]
}
此技术在应用程序处于前台时有效,但在后台时不起作用
安卓代码:
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());
}
list
<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>
最佳答案
每次我们在华为手机上遇到问题,都是因为该应用不是“ protected 应用”。
关于安卓火力地堡 : Huawei p8 push Notification is not shown in system when app is in background?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141950/
我是一名优秀的程序员,十分优秀!