如果后台运行应用程序,则不会触发OnMessageReceived [英] OnMessageReceived not fired if application in background
问题描述
我已经开发了一段时间的android应用程序,并且它们都已集成了FCM并且可以正常运行.
I've been developing android apps for a while now and all of them have FCM integrated in them and functioning properly.
我有一个此应用程序,除非打开该应用程序,否则不会触发onMessageReceived;如果该应用程序已关闭,并且我收到通知,它会从闪屏中打开并遵循正常流程(不会进入Notifications活动) . N.B:我所做的所有测试均来自Firebase控制台
I have this app that the onMessageReceived isn't fired unless the app is opened, if the app is closed and I receive a notification it opens from splash and follows the normal flow (it doesn't go to the Notifications activity). N.B: all the testing I've done was from the Firebase Console
我真的不知道为什么,将不胜感激.
I really don't know why, any help would be appreciated.
这是我的代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.wtf("RECEIVED","NOTIFICATION");
// TODO(developer): Handle FCM messages here.
Log.wtf(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.wtf(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.wtf(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification("Notification");
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
推荐答案
使用 Firebase ,您基本上可以发送两种类型的通知消息.
Using Firebase, you can send two type of notifications message basically.
1.通知消息:有时被称为显示消息".
1. Notification messages : sometimes thought of as "display messages."
2.数据/有效负载消息:由客户端应用处理.
2. Data/Payload messages : which are handled by the client app.
上述两种通知都有不同的行为,具体取决于您的应用是前台还是后台.
both above Notification has different behaviour, depending upon if your app is in foreground or background.
1.Notification msg + Background (通知消息+背景):已发送到通知托盘
1.Notification msg + Background : delivered to the notification tray
2.通知消息+前景:Android上的onMessageReceived()
2.Notification msg + Foreground : onMessageReceived() on Android
3.数据消息+后台:应用程序在通知托盘中接收通知有效载荷,并且仅在用户点击通知时处理数据有效载荷.
3.Data msg + Background : apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
4.Data msg + Foreground :您的应用会收到一个消息对象,其中两个有效负载都可用.
4.Data msg + Foreground : your app receives a message object with both payloads available.
有关更多说明,请选中此项.
要使用FCM控制台发送数据/有效载荷消息,请使用高级"选项并插入键和值.
For sending Data/payload message using FCM console use Advance option and insert key and value.
这篇关于如果后台运行应用程序,则不会触发OnMessageReceived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!