我想使用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>
答案 0 :(得分:0)
在实现FirebaseMessagingService类时遇到同样的问题
我的解决方案
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>
如果清单服务中的一切正常,那么
取消安装您的应用程序
文件->使缓存无效/重新启动...。
运行您的应用程序