应用被杀死后,如何从Firebase向设备发送消息? [英] How to send message from Firebase to device when app is killed?

查看:117
本文介绍了应用被杀死后,如何从Firebase向设备发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道文档说明:

如果您的应用程序处于前台或后台,则可以通过onMessageReceived方法接收消息,否则用户将在托盘中收到通知...单击它会打开主要活动,其中包含意图中的数据

但是,即使关闭了应用程序,有什么方法可以从通知控制台捕获每条消息?

==== 答案 ====

解决方案

onMessageReceived()方法 如果应用程序在后台或仅在收到消息时终止,则不会被调用通过Firebase控制台发送.

应用未运行时,您仍然会从Firebase控制台收到通知 .但是,如果您想通过 onMessageReceived()拦截数据,则必须创建一个自定义应用服务器,该服务器仅将发送 数据有效载荷发送到fcm端点.简而言之,如果您想在这种情况下更有效地利用FCM,则必须创建一个应用服务器.

您可以看看这两个问题,它们讨论了更多有关同一问题的内容,在第二个问题中,我还简要讨论了如何使用node.js和java servlet进行相同操作:

  1. 用户点击通知?
  2. 如何在Firebase有新条目时将通知推送到客户端?

请让我知道这是否为您提供了一些信息.

I am trying to get familiar with Firebase Notifications. It works fine, but I am stuck with receiving messages from the notification console when the app is not turned on.

I know that documentation says that:

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

But is there are any way to catch every message from the notification console even if the application is closed?

==== ANSWER ====

Find answer here

There is no way to send data message from the notification console.

But there are other ways to send notification to devices and they will be caught inside onMessageReceived!

You can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send

with the next body:

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

also you need to add 2 headers:

  1. Authorization - key=your_server_key_here
  2. Content-Type - application/json

To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

解决方案

onMessageReceived() method will not be called if the app is in background or killed only when the message is sent through Firebase Console.

When app is not running you will anyway receive notification from firebase console. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only data payload to fcm endpoint. In short you will have to create an app server if you want to actually utilize FCM more efficiently in this case .

You can have a look at these two questions which discuss more regarding same and in second question I also discuss briefly over using node.js and java servlet to do the same:

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

Do let me know if this provides you with some info.

这篇关于应用被杀死后,如何从Firebase向设备发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆