在我的 android 项目中,我包括了 Firebase 云消息传递。我在 SDK 21 (Android 5) 上。
我的 Intent 是重写 MyFirebaseMessagingService
的 onMessageReceived()
方法以捕获 Firebase 发送的方法并创建我自己的通知。
但似乎 onMessageReceived
无论如何都不会被调用。我按照 Android Studio 的内置说明来实现 firebase 并且使用 firebase 控制台进行测试实际上确实发送了我的测试设备收到的消息。
所以我的问题是,我是不是忘了实现某些东西?我有我的自定义 FirebaseMessagingService
,一个用于在通知中执行操作的 BroadcastReceiver
和一个为 >= 26 创建和处理 SDK 的通知 channel 的 Application
类.
这是我的 list 文件的一部分:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:name=".Networking.Firebase.FireBaseApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.MainActivity"/>
<activity
android:name=".Activities.SplashActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".Networking.Firebase.PushMessageService" />
<receiver android:name=".Networking.Firebase.PushReceiver"/>
</application>
最佳答案
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage?) {
super.onMessageReceived(message)
//TODO Your Code
}
}
}
<!-- Firebase Notifications Service -->
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!--
Set custom default icon. This is used when no icon is set for incoming notification messages.
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<!--
Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message.
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
关于android - 为什么 FirebaseMessagingService.onMessageReceived() 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53615599/