关闭应用程序后如何在Android通知中显示操作按钮? [英] How to display action buttons in Android notification when app is closed?

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

问题描述

我有一个Android应用.有了通知,我们必须显示一些操作按钮.当应用程序打开时,我们可以自由地构建通知并显示操作按钮.

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.

但是,当应用关闭时,通知会在android的通知托盘中收到.应用开发人员无权构建ui和操作按钮的地方.我们现在如何显示按钮? 如何从服务器端格式化数据,以便在应用程序中接收到数据时,我们可以看到操作按钮

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

推荐答案

根据我的经验,发生这种情况是因为您正在发送通知消息.正如google所解释的那样,此处明确

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.

现在,如果您要使用Firebase Cloud Messaging发送通知,并希望始终以自己的自定义方式显示收到的通知,则可以使用不包含notification部分的 FCM数据消息,例如这个:

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"
    }
  }
}

Firebase收到此通知后,不会将其显示在任务栏面板中,而是将其交付给您的应用程序(您的应用程序位于前台还是后台).然后,您可以在data部分,以创建和显示带有自定义操作的自定义通知.

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.

您还可以在data部分中放置任何字段,而title之类的字段只是示例.

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

客户端中的其他实施详细信息:

要在android中处理数据消息(本机和/或对本机做出反应),可以使用远程消息(但使用不同的方式).

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.

原生android: 在本机android中,您可以使用服务的onMessageReceived(RemoteMessage remoteMessage)方法(实现FirebaseMessagingService).然后按照此处所述,通过remoteMessage.getData()使用有效负载数据.

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.

这篇关于关闭应用程序后如何在Android通知中显示操作按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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