Android I';当新消息发送给用户且应用程序位于后台时,我没有收到推送通知
我正在使用Firebase数据库、云消息和Node.js在Android设备之间发送通知,让聊天应用程序在后台收到来自另一个应用程序的新消息时尝试获取推送通知 我在关注这个博客。这里是 [ 下面是我试过的代码Android I';当新消息发送给用户且应用程序位于后台时,我没有收到推送通知,android,node.js,firebase,push-notification,google-cloud-platform,Android,Node.js,Firebase,Push Notification,Google Cloud Platform,我正在使用Firebase数据库、云消息和Node.js在Android设备之间发送通知,让聊天应用程序在后台收到来自另一个应用程序的新消息时尝试获取推送通知 我在关注这个博客。这里是 [ 下面是我试过的代码 @Override public void onClick(View view) { if (view.getId() == R.id.btnSend) { String content = editWriteMessage.getText().toStri
@Override
public void onClick(View view) {
if (view.getId() == R.id.btnSend) {
String content = editWriteMessage.getText().toString().trim();
if (content.length() > 0) {
editWriteMessage.setText("");
Message newMessage = new Message();
newMessage.text = content;
newMessage.idSender = StaticConfig.UID;
newMessage.idReceiver = roomId;
newMessage.timestamp = System.currentTimeMillis();
FirebaseDatabase.getInstance().getReference().child("message/" + roomId).push().setValue(newMessage);
sendNotificationToUser(newMessage.idReceiver,newMessage.text);
}
}
}
public void sendNotificationToUser(String user, String message) {
Map notification = new HashMap<>();
notification.put("username", user);
notification.put("message", message);
FirebaseDatabase.getInstance().getReference().child("notificationRequests").push().setValue(notification);
}
最重要的是,我需要把上面的代码放在哪里才能让它工作
我还创建了一个bucket来承载node.js文件
提前感谢。编辑:我误解了您的问题。请确保您的manifest.xml文件中包含firebase消息服务。 原件: 使用
functions.database.ref().onCreate()
方法。在ref()参数中放置通知请求的路径
下面是TypeScript中的一些基本代码
export const notificationListener = functions.database
.ref('/NotificationRequests/{notification}').onCreate((snapshot, context) =>
{
try {
admin.initializeApp();
} catch (e) {
}
//Code to send notification here
return admin.database().ref('/NotificationRequests/' + context.params.notification)
}
)
通知所需的信息位于snapshot.child(“此处的路径”)
下
编辑:我误解了您的问题。请确保您的manifest.xml文件中包含firebase消息服务。 原件: 使用
functions.database.ref().onCreate()
方法。在ref()参数中放置通知请求的路径
下面是TypeScript中的一些基本代码
export const notificationListener = functions.database
.ref('/NotificationRequests/{notification}').onCreate((snapshot, context) =>
{
try {
admin.initializeApp();
} catch (e) {
}
//Code to send notification here
return admin.database().ref('/NotificationRequests/' + context.params.notification)
}
)
通知所需的信息位于snapshot.child(“此处的路径”)
下
我真的没有想法,请您提供一个代码片段好吗?我真的没有想法,请您提供一个代码片段好吗