Android 通过firebase云消息将数据从api发送到我的应用程序

Android 通过firebase云消息将数据从api发送到我的应用程序,android,Android,我有一个应用程序,可以从sport api中获取日期、时间、球队1和球队2等特定数据,我希望每次都使用FCM显示带有数据的通知(我的通知是针对英超足球直播比赛的,因此当比赛开始时,用户应用程序中应该会弹出一个通知),是否可以通过FCM将api中的数据发送到我的应用程序以显示通知,我已经读了一点关于FCM HTTP V1的内容,但我仍然不确定它是否能完成任务,有什么建议吗,伙计们,谢谢。您可以向订阅该主题的所有用户发送针对特定主题的JSON。 您可以通过调用以下命令订阅该主题中的用户 Fireba

我有一个应用程序,可以从sport api中获取日期、时间、球队1和球队2等特定数据,我希望每次都使用FCM显示带有数据的通知(我的通知是针对英超足球直播比赛的,因此当比赛开始时,用户应用程序中应该会弹出一个通知),是否可以通过FCM将api中的数据发送到我的应用程序以显示通知,我已经读了一点关于FCM HTTP V1的内容,但我仍然不确定它是否能完成任务,有什么建议吗,伙计们,谢谢。

您可以向订阅该主题的所有用户发送针对特定主题的JSON。 您可以通过调用以下命令订阅该主题中的用户

FirebaseMessaging.getInstance().subscribeToTopic("your_topic");
订阅后,您可以将此JSON发送到API

连同标题
“内容类型”、“应用程序/json”
“授权”、“密钥=您的\u FCM\u遗留\u服务器\u密钥”

如何处理NotificationService上的“数据”取决于您

这是处理JSON这部分数据的方式

"data": {
       "time": "your time"
       "date": "your date"
       "home_team": "your home team"
       "away_team": "your away team"
      }
像这样处理

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        super.onMessageReceived(remoteMessage);

        Map<String, String> data = remoteMessage.getData();

        String time= data.get("time");
        String date= data.get("date");
        String home_team= data.get("home_team");
        String home_away= data.get("home_away");

        String body = remoteMessage.getNotification().getBody(); //retrieve the notification body
        String title = remoteMessage.getNotification().getTitle(); //retrieve  the notification title

}
@覆盖
收到消息时公共无效(RemoteMessage RemoteMessage){
super.onMessageReceived(remoteMessage);
Map data=remoteMessage.getData();
字符串时间=data.get(“时间”);
字符串日期=data.get(“日期”);
String home_team=data.get(“home_team”);
String home\u away=data.get(“home\u away”);
String body=remoteMessage.getNotification().getBody();//检索通知正文
String title=remoteMessage.getNotification().getTitle();//检索通知标题
}

您可以基于此API中的函数发送JSON
https://fcm.googleapis.com/fcm/send
与您的Firebase project Legacy server密钥一起,使用Firebase MessagingService中扩展的NotificationService在android中捕获该密钥谢谢您的回复,您是否有任何代码显示如何执行该操作,我没有在谷歌上找到太多关于它的搜索抱歉这不是stackoverflow的工作方式,你问的是FCM通知的功能,它不能被一段代码共享。我建议您在android中创建第一个NotificationService,并在Firebase Cloud Messaging Dashboard中手动测试它,使其动态/自动。您可以触发一个异步任务,在目标特定用户、多用户或通过订阅发送JSON数据。topici已经实现了Firebase,一切正常,我创建了一个扩展FirebaseMessagingService的服务类,它覆盖了OnMessageReceived,我还准备好了我匹配的json对象,我只是不理解将json数据发送到google api linkComments的部分不用于扩展讨论;这段对话已经结束。
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        super.onMessageReceived(remoteMessage);

        Map<String, String> data = remoteMessage.getData();

        String time= data.get("time");
        String date= data.get("date");
        String home_team= data.get("home_team");
        String home_away= data.get("home_away");

        String body = remoteMessage.getNotification().getBody(); //retrieve the notification body
        String title = remoteMessage.getNotification().getTitle(); //retrieve  the notification title

}