【发布时间】:2019-08-20 09:50:22
【问题描述】:
我正在制作聊天应用程序,尝试在应用程序在后台使用 Android 设备之间使用 Firebase 数据库、云消息传递和 Node.js 发送通知时收到来自另一个用户的新消息时收到推送通知。
我正在关注这个博客。这里是
[https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html]
下面是我尝试过的代码。
@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);
}
通过使用上述代码,用户名和消息被实时保存在 notificationRequests 数据库中。
而且我真的不知道这行代码如何接收推送通知。
FirebaseMessaging.getInstance().subscribeToTopic("user_"+username);
最重要的是我需要把上面的代码放在哪里才能让它工作。
我还创建了存储桶来托管我的 node.js 文件。
提前致谢。
【问题讨论】:
标签: android node.js firebase push-notification google-cloud-platform