这是代码:
class LatestFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d("pikabo", token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
}
}
清单:
<service
android:name=".classes.LatestFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我一次也没拿到代币?!
我想我把它初始化对了,因为
val reliableIdentifier: String = FirebaseInstanceId.getInstance().id
活动中正在工作
我错过了什么??
获取firebase令牌的正确方法是使用addOnCompleteListener,如下所示
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { task ->
if (task.isSuccessful)
print(task.result?.token)
else
print(task.exception?.message)
}
查看官方文档