【问题标题】:I'm not getting Push Notification when new message send to user And app is in Background当新消息发送给用户并且应用程序在后台时,我没有收到推送通知
【发布时间】: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


    【解决方案1】:

    编辑:我误解了你的问题。确保 firebase 消息服务在您的 manifest.xml 文件中。 原版的: 使用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('your path here') 下。

    See why and how this works

    【讨论】:

    • 请你给我一个代码sn-p,因为我真的不知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多