android - 应用启动时未调用 FirebaseMessagingService
问题描述
我想使用 FirebaseMessagingService 来处理来自服务器的推送通知。但是一开始没有调用onCreate
函数。我认为该服务在应用程序启动时会自动初始化。此外,我开始从 firebase 云消息传递测试通知,但它没有用。
class PushNotificationService: FirebaseMessagingService() {
private lateinit var app: App
override fun onCreate() {
super.onCreate()
App.log("FireBaseMsg: starting service")
app = application as App
}
override fun onMessageReceived(msg: RemoteMessage?) {
super.onMessageReceived(msg)
App.log("FireBaseMsg: onMessageReceived")
val pNotification = msg?.notification
if (pNotification != null){
val title = pNotification.title
val text = pNotification.body
if (!title.isNullOrEmpty() && !text.isNullOrEmpty()){
val p = PushNotification(app, NOTIFICATION_CHANNEL_ID_PUSH, title = title, text = text)
p.fireNotification(NOTIFICATION_ID_PUSH)
}
}
}
override fun onNewToken(token: String) {
App.log("FireBaseMsg: Received token: $token")
//REGISTER TOKEN
app.regPushNotification(token, ::onNewTokenCallback)
}
private fun onNewTokenCallback(err: ApiCallError?){
if (err == null){
app.showToast(app.getString(R.string.notification_push_token_failed))
}
}
}
显现:
<service
android:name=".services.PushNotificationService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
解决方案
I was in same problem while i am implementing FirebaseMessagingService class
My solution
Manifest.xml
<service
android:name=".MyFirebaseMessagingServices"
android:enabled="true"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
If everything ok in your manifest service section then
Uninistall your application
File -> Invalidate Caches / Restart....
Run your application
推荐阅读
- c - 无效 *ptr = (int *)&a; 它不是对指针进行类型转换吗
- javascript - jQuery:选择标签期望一个ID
- c# - 选择要使用的 .NET Core 版本
- amazon-web-services - 数据库子网组子网 ID 的问题
- javascript - 当我通过路由器链接访问 URL 时,组件中的 Vue.js v-for 会呈现,但是如果我手动键入 URL 或刷新页面,则 v-for 不会呈现
- groovy - Groovy:关闭冒号标记?
- node.js - 如何从 Mongodb 集合中获取部分文档以通过滚动到 React 进行加载
- python - 如何设置 django_settings_module 环境变量
- javascript - 尝试将 firebase 导入 sapper 项目时,node_modules\idb\build\idb.js 错误未导出“deleteDb”
- c++ - Qt3DRenderer::QCamera WebAssembly 支持的替代方案