我有一个功能( Firebase功能),如果他收到消息,它会向特定设备发送通知。 该功能可以按预期工作,但是接收方永远不会在Firebase控制台日志中收到通知,甚至会,输出如下:
Successfully sent message: { results: [ { messageId: '0:1581972223155663%ca6aa243f9fd7ecd' } ],
canonicalRegistrationTokenCount: 0,
failureCount: 0,
successCount: 1,
multicastId: 7431837786357220000 }
这是功能代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// When a new message is added, we retrieve it's content
exports.sendNotification = functions.database.ref("/chats/{id}/messages/{messageId}")
.onCreate((change,context) => {
var content = change.val();
console.log("Content is = ", content);
// This is the user ID who is supposed to receive the sendNotification
// Note that this ISN'T the token....
var userWhoReceivedTheMessageId = content.receiver;
console.log("Receiver ID = ", userWhoReceivedTheMessageId);
// This is the payload. Title is a constant and the text is the message received.
var payload = {
data:{
title: "Stranger has sent you a message",
text: content.content
}
};
// Gets the token
const refSenderId = admin.database().ref("/users/" + userWhoReceivedTheMessageId + "/token");
return refSenderId.once('value')
.then(dataSnapshot => {
const token = dataSnapshot.val();
console.log("Token is: ", token);
return admin.messaging().sendToDevice(token, payload);
})
.then(function(response){
console.log("Successfully sent message: ", response);
return null;
})
.catch(function(error){
console.log("Error sending message: ", error);
return null;
})
});
而且,在android studio中,这是我可以随时检查通知的服务的地方:
class MyFirebaseInstanceId : FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
if(p0.data.size > 0){
val payload :Map<String, String> = p0.data
sendNotification(payload)
}
}
override fun onNewToken(p0: String) {
super.onNewToken(p0)
// save the token
FirebaseDatabase.getInstance().reference.child("users").child(MainActivity.auth.currentUser?.uid.toString()).child("token").setValue(p0)
Log.i("TOKEN", p0)
}
private fun sendNotification(payload: Map<String, String>) {
val builder = NotificationCompat.Builder(this)
builder.setSmallIcon(R.drawable.common_google_signin_btn_icon_disabled)
builder.setContentTitle(payload.get("title"))
builder.setContentText(payload.get("text"))
val intent = Intent(this, MainActivity::class.java)
val stackBuilder = TaskStackBuilder.create(this)
stackBuilder.addNextIntent(intent)
val resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(resultPendingIntent)
val notificationManager = (getSystemService(Context.NOTIFICATION_SERVICE)) as NotificationManager
notificationManager.notify(0, builder.build())
}
}
并在 manifest.xml 中:
<service
android:name=".MyFirebaseInstanceId">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
我不认为问题出在生成的令牌上,但这是我生成令牌的方式:
FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener { p0 ->
if (p0.isSuccessful){
token = p0.result?.token.toString()
}
}
为什么其他电话没有收到通知?