How to display action buttons in Android notification when app is closed?

旧城冷巷雨未停 提交于 2020-08-26 09:13:43

问题


I have an android app. With notifications we have to display some action buttons. When the app is open we have the liberty to construct the notification and display the action buttons.

But when app is closed the notification is received in notification tray in android. Where an app developer don't have control to build ui and action buttons. how can we show the buttons now ? How the data is formatted from server side so that when it is received in app , we can see action buttons


回答1:


Based on my experience, this occurs because you are sending a Notification Message. As google explains it here clearly:

Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by a callback function.

Now if you want to send your notification using Firebase Cloud Messaging and want to always display received notification in your own custom way, you can use FCM data messages that not contains notification part, like this:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "priority":"high",
    "data":{
      "title" : "Custom notif",
      "body" : "This is a custom data notification!",
      "action" : "A|B|C"
    }
  }
}

After receiving this notification, the Firebase will not display it in tray panel, instead will deliver it to your app (either your app is in foreground or in background). Then you can use fields in the data section to create and display custom notification with custom actions.

Also you can put any field inside data section and the fields like title are just examples.

Extra implementation details in client:

For processing data messages in android (native and/or react native), you can use remote messages (but with different ways).

React native: For process data only notifications in react native you can use this example.

Native android: In the native android you can use onMessageReceived(RemoteMessage remoteMessage) method of your service (that implements FirebaseMessagingService). And then use payload data by remoteMessage.getData() as explained here.



来源:https://stackoverflow.com/questions/59733439/how-to-display-action-buttons-in-android-notification-when-app-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!