简体   繁体   English

FCM onMessageReceived未调用Android

[英]FCM onMessageReceived not calling Android

I am facing one problem while implementing FCM in Android application. 在Android应用程序中实施FCM时遇到一个问题。

Everything works fine, my service onMessageReceived() is not calling. 一切正常,我的服务onMessageReceived()没有调用。 But I am getting following logs when I send a message from my Java Server. 但是,当我从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

Any suggestions ???? 有什么建议么 ????

This is my service class, 这是我的服务班

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 file, 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>

I am getting the Notifications while the app is in the background. 我在后台运行应用程序时收到通知。 Problem occurs if application comes in foreground 如果应用程序进入前台,则会出现问题

First , follow instructions from this guide, they are very clear and easy, or you can clone sample code from here and see for yourself what is wrong. 首先 ,请按照指南中的说明进行操作, 这些说明非常简单明了,或者您可以从此处克隆示例代码,亲自了解问题所在。

Secondly have you read these comments regarding onMessageReceived() 其次 ,您是否阅读了有关onMessageReceived()注释

// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]

Look for following comment in this class 在本课程中寻找以下评论

// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.

For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 有关更多信息,请参见: https : //firebase.google.com/docs/cloud-messaging/concept-options

Firebase will not call your onMessageReceived() when your app is in the background or killed, and you can't customise your notification. 当您的应用在后台或被杀死时,Firebase不会调用您的onMessageReceived() ,并且您无法自定义通知。 It will show notifications managed by Firebase it self. 它将显示由Firebase自行管理的通知。

to make Firebase library to call your onMessageReceived() in every case 使Firebase库在每种情况下都可以调用您的onMessageReceived()

a) Foreground a)前景

b) Background b)背景

c) Killed c)被杀

Do not put JSON key "notification" in your request to Firebase API, use "data" instead , see below. 请勿在对Firebase API的请求中放入JSON键“ notification”,而应使用“ data” ,请参见下文。

For example, following message will not call onMessageReceived() 例如,以下消息将不会调用onMessageReceived()

{
  "to": "/topics/test",
  "notification": {
    "title" : "title",
    "message": "data!"
   }
}

but this will work 但这会起作用

{
  "to": "/topics/test",
   "data": {
       "title":"title",
       "message":"data!"
   }
} 

see this it has a detailed description of firebase message type For example: 看到它对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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM