如何在应用程序关闭时使用Android Studio中的Firebase发送通知? [英] How to send notifications using Firebase in Android Studio when the app is closed?

查看:436
本文介绍了如何在应用程序关闭时使用Android Studio中的Firebase发送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio和Firebase。我想在其他用户向他发送消息时向特定的用户发送通知,并且我希望他的手机能够得到这些通知,即使他的应用程序根本没有运行。



我试图通过firebase本身,YouTube教程和谷歌来使用一些公会。但是我失败了。有人可以帮我一下吗?

FCM有两种消息:下游和<强>通知即可。每种类型的行为都不同,具体取决于应用程序状态。



1。通知



只有当应用程序处于后台时,通知才会被设备接收。它会显示或在通知托盘,或作为抬头通知。当您的应用程序处于前台时,您不能收到 Notification 类型的消息。



2。下游



您的应用程序可以在任何状态下收到下游消息。您将在 FirebaseMessagingService 实现( onMessageReceived )中收到它。

3。两个世界



如果将1和2组合,您将得到下游通知消息。所以它会从两个世界采取行为。如果您的应用在后台,您将收到通知消息,如果您的应用处于前台,您将收到 Downstream 另外,如果您点击通知,指定 click_action ,并且您有 Intent Filter 引导你到 Activity 并调用 onNewIntent onCreate 取决于在后台。所以,回答你的问题 - 如果你想总是收到新的消息通知 - 只发送 Downstream 消息。即使应用程序被杀了,它也会收到它,或者当应用程序将活着(如果消息服务被启用重新启动),或马上。但是这样做有一个缺点:你需要自己向用户显示通知(HeadsUp或者定期)。

更新



以下是常见的Firebase讯息正文:

  {
to:FIREBASE_TOKEN,
notification:{//负责通知类型的消息
body: b $ b,b $ b,b $ b,$对于下游类型的消息
Nick:Mario,
Room:PortugalVSDenmark
}
}

那么你需要什么 - 它以某种方式(如json,或者这样)将这些数据发送到指定的 firebase url 与一些标题。 这里是详细说明如何发送消息的链接。请注意,有信息说明如何发送多个设备/组的消息,并且只需要在中只指定您的合作伙伴 FirebaseToken 即可:字段。

I'm using Android Studio and Firebase. I want to send to a specific user a notification when other users sends him messages, and I want his phone to be able to get these notifications, even when his app is not running at all.

I've tried to use some guilds by firebase themselves, youtube tutorials and google. But I failed. Can someone please help me with this?

解决方案

FCM have two kinds of messages: downstream and notification. Each of this types behave differently depending on the app state.

1. Notification

Notification will be received by device only if app is in background. It'll be displayed or in notifications tray, or as Heads Up notification. You can't receive Notification kind of message when your app is in foreground.

2. Downstream

Downstream message will be received by your app in any state. You'll receive it in your FirebaseMessagingService implementation (onMessageReceived).

3. Both worlds

If you combine both, 1 and 2, you'll get downstream-notification message. So it'll take behavior from both "worlds". You'll receive Notification message if your app is in background and Downstream if your app is in foreground. Also if you'll click on notification where click_action is specified and you have Intent Filter for this action - it'll lead you to that Activity and call onNewIntent or onCreate depending on backstack.

So, answering your question - if you want to always receive new message notification - send only Downstream message. Even if app is killed it'll receive it or when app will become alive (if messaging service was enable to restart), or right away. But there is one drawback in this way: you'll need to show notification (HeadsUp or regular) to user by yourself.

Update

Here is common Firebase message body:

 {
    "to" : "FIREBASE_TOKEN",
    "notification" : {  // Responsible for Notification kind of messages
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : { // Responsible for Downstream kind of messages
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

So what do you need - it's in some way (as json, or so) send this data to specified firebase url with some headers. Here is link with details how to send messages. Note that there is info how to send message top multiple devices/groups and you need to specify only your partner FirebaseToken in to: field.

这篇关于如何在应用程序关闭时使用Android Studio中的Firebase发送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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