未生成Firebase消息传递令牌


class MyFirebaseInstanceIDService : FirebaseMessagingService() {
override fun onNewToken(p0: String) {
super.onNewToken(p0)
Log.i("token", p0)
}

}

这是我用来生成令牌的代码。

清单:

<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

我已经重新安装了应用程序,但仍然找不到令牌

更新:我现在可以在onNewToken(token: String)方法中接收令牌,但不执行onMessageRecieved(message: RemoteMessage)方法。

我正在使用改造来提出网络请求,并且响应是成功的。

改装电话:

private fun sendNotification(sendNotification: SendNotification) = CoroutineScope(Dispatchers.IO).launch {
Log.i("sendNotification", "came here")
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com/").addConverterFactory(GsonConverterFactory.create()).build()
val pushNotification: PushNotification = retrofit.create(PushNotification::class.java)
val response = pushNotification.send(sendNotification)
if (response.isSuccessful){
Log.i("response", response.toString())
}else{
Log.i("response", response.errorBody()!!.toString())
}
}

发送通知:

data class SendNotification(
var notification: NotificationData,
var to: String = ""
): Serializable {
}

通知数据:

data class NotificationData(
var title: String = "",
var message: String = ""
): Serializable {
}

onMessageReceived:

private const val CHANNEL_ID = "channel_id"
class MyFirebaseMessagingService: FirebaseMessagingService() {

@RequiresApi(Build.VERSION_CODES.O)
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val notificationManager: NotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT)
val notificationId = Random.nextInt()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createNotificationChannel(notificationManager)
}
Log.i("onMessageReceived", message.toString())
val notification = Notification.Builder(this, CHANNEL_ID)
.setAutoCancel(true)
.setSmallIcon(android.R.drawable.sym_def_app_icon)
.setContentTitle(message.data["title"])
.setContentText(message.data["message"])
.setContentIntent(pendingIntent)
.build()
notificationManager.notify(1, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(notificationManager: NotificationManager){
val notificationChannel = NotificationChannel(CHANNEL_ID, "messagingexample", NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(notificationChannel)
}
}

确保您提供的密钥是准确的,并且在Firebase Dashboard上启用了云消息。

可能是您的服务没有运行,请尝试"活动",看看它是否记录了任何内容。

我的示例代码

FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() { @Override public void onComplete(Task<InstanceIdResult> task) { final boolean _success = task.isSuccessful(); final String _token = task.getResult().getToken(); final String _errorMessage = task.getException() != null ? task.getException().getMessage() : ""; String ref = ""; if (_success) { ref = "fcm_token/".concat(FirebaseAuth.getInstance().getCurrentUser().getUid().concat("/token")); _firebase.getReference(ref).setValue(_token); ref = "fcm_token/".concat(FirebaseAuth.getInstance().getCurrentUser().getUid().concat("/uid")); _firebase.getReference(ref).setValue(FirebaseAuth.getInstance().getCurrentUser().getUid()); sp.edit().putString("fcm_token", _token).commit(); ref = "wallet/".concat(sp.getString("addr", "").concat("/token")); _firebase.getReference(ref).setValue(_token); ref = "wallet/".concat(sp.getString("addr", "").concat("/uid")); _firebase.getReference(ref).setValue(FirebaseAuth.getInstance().getCurrentUser().getUid()); } else { _warningToasty(_errorMessage); } } });

我在Manifest中有这个服务条目。

<service
android:name=".AppFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

相关内容

  • 没有找到相关文章

最新更新