【发布时间】:2020-05-31 14:00:30
【问题描述】:
我正在开发一个聊天应用程序,因此我需要发送已收到新消息的通知。 为此,我使用 Firebase 函数。 我正在使用 sendToDevice 功能,它需要一个 token 来发送通知。问题是,我似乎无法检索发送消息的用户的令牌。 这是我的 .js 代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref("/chats/{id}/messages/{messageId}/content")
.onWrite((change,context) => {
var content = change.after.val();
var payload = {
data:{
title: "Stranger has sent you a message",
text: content
}
};
// Here I need to the ID of the person who sent the message
// And then compare this Id with the two Ids of the to users that are in the conversation
// If the two Ids are different, then save the other Id as the token
// So that I can send a notification to the other user.
const senderId = database.ref("/chats/{id}/messages/{id}/sender/{senderId}");
admin.messaging().sendToDevice(senderId, payload)
.then(function(response){
console.log("Successfully sent message: ", response);
return null;
})
.catch(function(error){
console.log("Error sending message: ", error);
})
});
如您所见,我正在检查 messages/content 子项中的任何更改。 这就是我的通知内容。
然后,我正在尝试检索消息发件人 ID,以便知道是谁发送了消息并检索其他用户 ID 以通知他。
这可能有点令人困惑,所以这是我的 Firebase 实时数据库:
我做错了什么,所以这段代码可以正常工作?这是我在 android 中接收消息的活动:
class MyFirebaseInstanceId : FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
if(p0.data.size > 0){
val payload :Map<String, String> = p0.data
sendNotification(payload)
}
}
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("username"))
builder.setContentText(payload.get("email"))
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())
}
}
【问题讨论】:
-
请注意,使用
database.ref("/chats/{id}/messages/{id}/sender/{senderId}"),您获取的不是字段值,而是Reference。您需要使用once() 方法。另外注意需要返回admin.messaging().sendToDevice(senderId, payload)...,见firebase.google.com/docs/functions/terminate-functions -
@RenaudTarnec 你能给我一个如何使用
.once()函数的例子吗?这解决了我的问题,我想给你一个绿色的复选标记。
标签: javascript android firebase firebase-realtime-database google-cloud-functions