android - 应用程序被杀死时如何从 Firebase 向设备发送消息?

标签 android firebase firebase-cloud-messaging firebase-notifications

<分区>

我正在尝试熟悉 Firebase 通知。它工作正常,但当应用程序未打开时,我无法从通知控制台接收消息。

我知道文档说:

if your app in foreground or background you can receive message in onMessageReceived method, otherwise user will receive notification in tray... click on it will open main activity with data inside intent

但有没有什么方法可以在应用程序关闭的情况下从通知控制台捕获每条消息?

==== 回答 ====

寻找答案 here

无法从通知控制台发送数据消息。

但是还有其他方法可以向设备发送通知,它们将被捕获在 onMessageReceived 中!

您可以使用终端(Mac 或 Linux)或像 Postman 这样的服务在这个链接上发送 Post 请求:https://fcm.googleapis.com/fcm/send

与下一个主体:

{
    "to": "/topics/your_topic_here",
   "data": {
       "text":"text",
       "text1":"text1",
       ...
   }
}

您还需要添加 2 个标题:

  1. 授权 - key=your_server_key_here
  2. 内容类型 - application/json

要获取您的服务器 key ,您可以在 firebase 控制台中找到它:您的项目 -> 设置 -> 项目设置 -> 云消息传递 -> 服务器 key

enter image description here

enter image description here

最佳答案

onMessageReceived() 方法如果应用程序在后台或仅当消息通过 Firebase 控制台发送时被杀死,则不会被调用。

当应用程序未运行时,您无论如何都会收到来自 firebase 控制台的通知。但是,如果您想通过 onMessageReceived() 拦截数据,那么您将必须创建一个自定义应用程序服务器,该服务器将只发送 数据 负载到 fcm端点。简而言之,如果您想在这种情况下更有效地实际利用 FCM,则必须创建一个应用程序服务器。

你可以看看这两个问题,它们讨论了更多关于相同的问题,在第二个问题中,我也简要讨论了使用 node.js 和 java servlet 来做同样的事情:

  1. Handle the data payload without user tapping on the notification?
  2. How to push notification to client when Firebase has a new entry?

如果这为您提供了一些信息,请告诉我。

关于android - 应用程序被杀死时如何从 Firebase 向设备发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39777179/

相关文章:

javascript - 如果 parent 未知,则获取子对象 id = x

node.js - 云存储时 `node_modules`目录如何处理?

android - 什么时候 FirebaseInstanceId.getInstance().getToken() = null?

android - 如何在比较用户在android中输入的值时忽略数据库中的空值

android - Google fit Recording API延迟

android - Ant 编译错误 - NavUtils

java - 如何使用 Firestore 仅存储当前登录用户的数据

swift - 在 swift 中更新 Firebase 最新版本后出现错误 'Cannot find ' Messaging' in scope'

android - FCM 安卓数据报文

android - 如何设置滑动面板默认状态折叠和折叠面板最小高度?