【发布时间】:2019-11-13 07:30:51
【问题描述】:
我在我的应用程序中使用 FCM,在 onmessagerecieve() 函数中我执行一些功能。如果远程消息是通知或数据消息,我会实现这两种方法。
我的应用中有四种情况
- 当应用程序在前台时接收通知消息(它可以正常工作)
- 当应用程序在前台时接收数据消息(它可以正常工作)
- 当应用程序在后台时接收通知消息(它可以正常工作)
- 当应用处于后台时接收数据消息(这给我带来了问题)
问题是当我收到数据消息并且应用程序处于后台时......没有任何通知或执行原始功能都没有发生。 当应用程序处于前台时,我想在收到数据消息时执行相同的功能
public void onMessageReceived(RemoteMessage remoteMessage) {
Map data = remoteMessage.getData();
RemoteMessage.Notification notification = remoteMessage.getNotification();
if (data.isEmpty()) {
System.out.println("FCM type is Notification");
parseNotificationMessage(remoteMessage);
} else {
System.out.println("FCM type is Data");
parseDataMessage(remoteMessage);
}
}
private void parseNotificationMessage(RemoteMessage remoteMessage) {
String notification_title =remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
String first_letter = "";
String title = "";
try {
first_letter = notification_title.substring(0, 1);
title = notification_title.substring(1);
}
catch (Exception e){
}
if (first_letter.equals("@")&& title.equals("NewOrder")) {
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
Intent intent = new Intent("JSON_DATA");
Bundle bundle = new Bundle();
bundle.putString("Notification_JSON",notification_message );
bundle.putString("Signal","NewOrder");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else if (first_letter.equals("@")&& title.equals("CancelOrder")) {
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
Intent intent = new Intent("JSON_DATA");
Bundle bundle = new Bundle();
bundle.putString("Notification_JSON",notification_message );
bundle.putString("Signal","CancelOrder");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else {
generateNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
}
}
private void parseDataMessage(RemoteMessage remoteMessage) {
String notification_title =remoteMessage.getData().get("title");
String notification_message = remoteMessage.getData().get("message");
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
String first_letter = "";
String title = "";
try {
first_letter = notification_title.substring(0, 1);
title = notification_title.substring(1);
}
catch (Exception e){
}
if (first_letter.equals("@")&& title.equals("NewOrder")) {
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
Intent intent = new Intent("JSON_DATA");
Bundle bundle = new Bundle();
bundle.putString("Notification_JSON",notification_message );
bundle.putString("Signal","NewOrder");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else if (first_letter.equals("@")&& title.equals("CancelOrder")) {
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
Intent intent = new Intent("JSON_DATA");
Bundle bundle = new Bundle();
bundle.putString("Notification_JSON",notification_message );
bundle.putString("Signal","CancelOrder");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
else {
generateNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));
System.out.println("1234567:: "+notification_title);
System.out.println("1234567:: "+notification_message);
}
}
【问题讨论】:
-
您应该在这里查看我的旧答案:stackoverflow.com/questions/58586867/…
-
@JohnLe 实际上是在消息类型为数据且应用程序处于后台时运行 onmessagerecieve() .. 可能吗?
-
在哪个版本的android设备中产生问题(低于orio或来自orio和高于orio)
-
低于奥利奥目前我正在棉花糖上测试
-
DataMessagepush 将被接收应用程序后台/前台而不是NotificationMessagepush
标签: android firebase-cloud-messaging