当日志显示没有错误时,为什么我的推送通知没有出现在设备上?
Why isn't my Push Notification appearing on the device when the log shows that there is no error?
我已经部署了以下 firebase 函数,用于在我的 cloud firestore 聊天应用程序中实现推送通知。看完 this tutorial.
后我第一次这样做
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotificationToTopic = functions.firestore.document('chats/{roomid}/messages/{uid}').onWrite(async(event)=>{
let content = event.after.get('message')
var message ={
notification: {
title: 'You have an unread message',
body: content,
},
topic:'namelesscoder',
};
});
在测试功能时,log 显示功能正常工作,没有任何错误,但我的设备实际上没有收到通知。这是因为我声明数据库结构的方式吗?上面提到的代码中的路径是存储我的聊天消息的位置。
此功能会将推送通知发送到主题(订阅主题的用户组)而不是特定用户。如果您想向一组用户发送通知,您可以使用此功能并且您的用户必须订阅主题。在此功能中,主题为'namelesscoder',因此您的用户需要订阅此主题。
如果您在 android,请编写以下代码以订阅主题 'namelesscoder'
FirebaseMessaging.getInstance().subscribeToTopic("namelesscoder")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "subscribed to topic nameless coder.";
if (!task.isSuccessful()) {
msg = "Failed to subscribe to topic";
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
如果您想向特定用户发送通知,则必须将其发送到 FCM 令牌。
我已经部署了以下 firebase 函数,用于在我的 cloud firestore 聊天应用程序中实现推送通知。看完 this tutorial.
后我第一次这样做const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotificationToTopic = functions.firestore.document('chats/{roomid}/messages/{uid}').onWrite(async(event)=>{
let content = event.after.get('message')
var message ={
notification: {
title: 'You have an unread message',
body: content,
},
topic:'namelesscoder',
};
});
在测试功能时,log 显示功能正常工作,没有任何错误,但我的设备实际上没有收到通知。这是因为我声明数据库结构的方式吗?上面提到的代码中的路径是存储我的聊天消息的位置。
此功能会将推送通知发送到主题(订阅主题的用户组)而不是特定用户。如果您想向一组用户发送通知,您可以使用此功能并且您的用户必须订阅主题。在此功能中,主题为'namelesscoder',因此您的用户需要订阅此主题。
如果您在 android,请编写以下代码以订阅主题 'namelesscoder'
FirebaseMessaging.getInstance().subscribeToTopic("namelesscoder")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "subscribed to topic nameless coder.";
if (!task.isSuccessful()) {
msg = "Failed to subscribe to topic";
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
如果您想向特定用户发送通知,则必须将其发送到 FCM 令牌。