使用云消息传递firebase推送通知

时间:2018-04-10 13:46:33

标签: android firebase firebase-cloud-messaging

我正在使用来自firebase的云消息传递与我的Android应用程序,我正在尝试接收实时更新而不刷新活动并从数据库获取数据,因此当用户向另一个用户发送消息时,最后一个用户将收到通知,谁知道如何引导我朝正确的方向发展?

1 个答案:

答案 0 :(得分:0)

在Firebase服务FirebaseMessagingService中,您实施方法onMessageReceived,以便您可以根据通知中的信息更新聊天。

例如,要从通知消息中获取所需的数据:

String sender = null;
int chatId;
String msg = null;
public void onMessageReceived(RemoteMessage remoteMessage) {
    //YOU NEED TO USE DATA MESSAGES TO CARRY ALL THE INFORMATION ABOUT THE MESSAGE
    Map<String,String> data = remoteMessage.getData();
    title = data.get("sender");
    msg = data.get("body");
    chatId = data.get("chatId");
    //this function implements the updating of the chat with the new message received
    updateChat(chatId,sender,msg);
}

此代码示例基于推送通知消息数据有效负载,如下所示:

{
    "Message": {
            "Token": client_device_token,
            "Data": {
                    "sender": "John",
                    "chatId": chatId_between_me_and_Jhon,
                    "body": "Hi you, this is John"
                    }
                }

 }

方法updateChat(chatId,sender,msg)的实现取决于您,并取决于您的应用程序和数据库的逻辑。

相关问题