简体   繁体   中英

Sending notification Firebase with “data” and “notification”

What is the best way to send a notification with firebase? If i use only notification parameter, am i sure that this will arrive when the app is closed, in background, or foreground? I read somewhere that if you use data parameter the notification is always received. But in my phone seems the same with only the notification parameter. What should i use? My priority is that the notification always show up. Thanks!

On the page here you can see the answer to how it is handled according to the use of the notification or data fields.

Anyway, these two fields are used for different purposes:

  • notification contains, simplifying, the title and the text of the notification that should appear in the system tray. If the notification contains only this field, then if the app is in background, then it'll be shown in the system tray, if it's in the foreground it'll be passed to a method, and then you can decide what to do (including to show it)
  • data contains a list of key-value pairs, that should be directly used by your app. If your notification contains only this field, the notification will be passed to your app directly, independently if the app is in the foreground or the background

Finally, if it contains both, then if the app is in the foreground, then the notification is passed to your app and you can decide what to do with it (including showing it, as it seems you want), and if it's in the background it'll be shown in the system tray and the data part is delivered in the extras of the intent of the launcher Activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM