FCM onMessageReceived未调用Android
在Android应用程序中实现FCM时,我面临一个问题 一切正常,我的服务FCM onMessageReceived未调用Android,android,android-service,firebase-cloud-messaging,Android,Android Service,Firebase Cloud Messaging,在Android应用程序中实现FCM时,我面临一个问题 一切正常,我的服务onMessageReceived()没有调用。但是,当我从Java服务器发送消息时,我会收到以下日志 08-25 12:14:53.216 20482-20482/com.cognisun.sas D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x100
onMessageReceived()
没有调用。但是,当我从Java服务器发送消息时,我会收到以下日志
08-25 12:14:53.216 20482-20482/com.cognisun.sas D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.cognisun.sas cmp=com.cognisun.sas/com.google.firebase.iid.FirebaseInstanceIdReceiver (has extras) }, receiver=com.google.firebase.iid.FirebaseInstanceIdReceiver@ac9047a
08-25 12:14:53.222 20482-20482/com.cognisun.sas D/ActivityThread: BDC-Calling onReceive end receiver=class com.google.firebase.iid.FirebaseInstanceIdReceiver
08-25 12:14:53.223 20482-20482/com.cognisun.sas D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.cognisun.sas (has extras) } packageName=com.cognisun.sas resultCode=-1 resultData=null resultExtras=null}
08-25 12:14:53.223 20482-20482/com.cognisun.sas D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@750442b className=com.cognisun.sas.services.firebase.MyFirebaseMessagingService packageName=com.cognisun.sas intent=null}
08-25 12:14:53.223 20482-20482/com.cognisun.sas D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@750442b className=com.cognisun.sas.services.firebase.MyFirebaseMessagingService packageName=com.cognisun.sas intent=null}
08-25 12:14:53.224 20482-20482/com.cognisun.sas D/ActivityThread: SVC-Calling onStartCommand: com.cognisun.sas.services.firebase.MyFirebaseMessagingService@6345088, flags=0, startId=1
08-25 12:14:53.225 20482-20482/com.cognisun.sas D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@750442b startId=1 args=Intent { act=com.google.firebase.MESSAGING_EVENT pkg=com.cognisun.sas cmp=com.cognisun.sas/.services.firebase.MyFirebaseMessagingService (has extras) }}
08-25 12:14:53.227 20482-21308/com.cognisun.sas D/SettingsInterface: from settings cache , name = cover_app_component , value = disable
08-25 12:14:53.227 20482-21308/com.cognisun.sas D/SettingsInterface: from settings cache , name = cover_app_lock , value = 0
08-25 12:14:53.232 20482-20482/com.cognisun.sas D/ActivityThread: SVC-Destroying service: com.cognisun.sas.services.firebase.MyFirebaseMessagingService@6345088
08-25 12:14:53.233 20482-20482/com.cognisun.sas D/ActivityThread: SVC-STOP_SERVICE handled : 0 / android.os.BinderProxy@750442b
有什么建议吗
这是我的服务课
class MyFirebaseMessagingService : FirebaseMessagingService() {
internal var TAG = "FCM Message"
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.e(TAG, "From: " + remoteMessage!!.getFrom())
// Check if message contains a data payload.
if (remoteMessage.getData().size > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData())
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()!!)
}
}
}
AndroidManifest.xml文件
<service android:name=".fcm.MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我在应用程序处于后台时收到通知。若应用程序首先出现在前台,则会出现问题,请按照指南中的说明操作,这些说明非常清晰和简单,或者您可以从中克隆示例代码,并亲自查看错误所在 其次您是否阅读了关于
onMessageReceived()
请在本文中查找以下评论
有关详细信息,请参阅:当您的应用程序处于后台或被杀死时,Firebase将不会调用您的
onMessageReceived()
,并且您无法自定义通知。它将显示Firebase It self管理的通知
使Firebase库在任何情况下都能调用您的onMessageReceived()
a) 前景
b) 背景
c) 杀死
不要在对Firebase API的请求中添加JSON键“通知”,而是使用“数据”,请参见下文
例如,下面的消息将不会调用onMessageReceived()
但这会奏效的
{
"to": "/topics/test",
"data": {
"title":"title",
"message":"data!"
}
}
请参阅此页,其中包含firebase消息类型的详细说明,例如:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message").toString(),
remoteMessage.getData().get("title").toString());
}
}
private void sendNotification(String message, String title) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, activityCompat);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.small_logo)
.setContentTitle(title)
.setContentText(message).setContentIntent(pendingIntent)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setTicker(messageBody);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Notification notification = notificationBuilder.build();
notificationManager.notify(requestID, notification);
}
你们能发布清单文件吗..可能有助于增加清单..当我的应用程序在后台时,我收到通知,但当应用程序在前台时,我面临的问题完全有意义!
{
"to": "/topics/test",
"notification": {
"title" : "title",
"message": "data!"
}
}
{
"to": "/topics/test",
"data": {
"title":"title",
"message":"data!"
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message").toString(),
remoteMessage.getData().get("title").toString());
}
}
private void sendNotification(String message, String title) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(this, activityCompat);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.small_logo)
.setContentTitle(title)
.setContentText(message).setContentIntent(pendingIntent)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setTicker(messageBody);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Notification notification = notificationBuilder.build();
notificationManager.notify(requestID, notification);
}