问题描述
我在 Android 应用程序中实现 FCM 时遇到了一个问题.
I am facing one problem while implementing FCM in Android application.
一切正常,我的服务 onMessageReceived()
没有调用.但是当我从 Java 服务器发送消息时,我会收到以下日志.
Everything works fine, my service onMessageReceived()
is not calling. But I am getting following logs when I send a message from my Java Server.
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 文件,
AndroidManifest.xml file,
<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>
我在应用处于后台时收到通知.如果应用程序进入前台会出现问题
推荐答案
首先,按照 this 指南,它们非常清晰和简单,或者您可以从 这里 自己看看哪里出了问题.
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.
其次你读过这些关于onMessageReceived()
的评论吗
Secondly have you read these comments regarding 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]
在此 类
// 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.
有关更多信息,请参阅:https://firebase.google.com/docs/cloud-messaging/concept-options
For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
这篇关于FCM onMessageReceived 未调用 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!