我在我的应用程序中使用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);
}
}
在棉花糖下面,您必须使用NotificationCompat。生成器向您显示消息,并且您必须使用NotificationChannal才能高于或等于棉花糖
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(verseurl);
bigText.setBigContentTitle("Title");
bigText.setSummaryText("Text in detail");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, mBuilder.build());
在以下情况下,使firebase库调用onMessageReceived((
前台应用程序
后台应用程序
应用程序已被杀死
您不能将JSON密钥"notification"放在对firebase API的请求中,而是使用"data"。
当您的应用程序处于后台时,如果您同时发送通知和数据消息,则不会在onMessageReceived()
内收到任何通知消息或数据消息。通知消息将作为通知发送到系统托盘中,数据消息将发送到单击通知时启动的活动意图的附加部分。您可以做的是只发送带有通知标题和说明的数据消息,这样您就可以将其显示为通知或执行任何您想要的操作。另一方面,当从如下的附加意图点击通知时,您可以处理您的数据消息:
从".MainActivity"的onCreate中的通知中获取"数据"信息:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get notification data info
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
//Handle data
}
}