我有一个应用程序使用Firebase消息传递服务来接收推送通知。如何在销毁应用程序时使用Firebase显示推送通知
我已经搜索了向FCM发送服务器发送通知信息时使用“通知”或“数据”的区别。
当应用程序被销毁时,只有当它在前景或背景上时,才会显示我的通知。
我需要这样做才能让我的服务器每隔一段时间(大约每周两次左右)发送推送通知给设备,但应用程序应该能够在关闭时显示通知。
任何想法我可以做到这一点?这是我通知JSON编码是我发送给FCM发送服务器字符串:
{
"to":"d_kQKDkq5V4:APA91bG4hIyqbr2a_24nUIe6jVbySS90FMnVKwwuTfG1dV3OooeAc_555XkB2e_h_oWzEOMde8uIt2ESvv_Thl1J1sEXDHWPsLCE7EdMXkZ_AS6xlVq8uJIjVojz1WPxqiUjWZm65Ypf",
"data":{
"title":"App in background again",
"body":"asdsad"
}
}
因此而应用程序是在背景或前景,但是当它被摧毁它不会出现在通知出现。我该如何做到这一点,所以它仍然可以接收并显示通知,而应用程序被销毁?
这是我onMessageRecieved()方法:
public void onMessageReceived(RemoteMessage remoteMessage) {
/*String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
*/
Log.e("REG_TOKEN",remoteMessage.getData().get("title"));
Map<String,String> data = remoteMessage.getData();
String title = data.get("title");
String message = data.get("body");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
我增加了优先级,但没有改变我仍然无法接收通知。然后我开始在停止状态问题上回顾并在这里回答stackoverflow,但我似乎没有得到我需要实际完成此工作的类。我需要使用BroadcastReciever吗? –
杀死应用程序后,请转到_Settings_> _App Manager_。找到应用程序并查看应用程序信息。 _Force Stop_按钮是启用还是禁用?如果禁用,该应用程序处于停止状态,并且不会收到FCM消息,直到用户通过打开状态将其从停止状态中取出为止。 –
强制停止不被禁用,我仍然可以点击它。那意味着它不处于停止状态? –