如何在应用程序关闭时在 Android 通知中显示操作按钮?
How to display action buttons in Android notification when app is closed?
我有一个 android 应用程序。对于通知,我们必须显示一些操作按钮。当应用程序打开时,我们可以自由构建通知并显示操作按钮。
但是当应用程序关闭时,通知会在 android 的通知托盘中收到。应用程序开发人员无法控制 build ui 和操作按钮。我们现在如何显示按钮?
如何从服务器端格式化数据,以便在应用程序接收到数据时,我们可以看到操作按钮
根据我的经验,发生这种情况是因为您正在发送 通知消息。正如 google 所解释的 here 清楚:
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 云消息传递发送通知并始终以您自己的自定义方式显示收到的通知,您可以使用 FCM 数据消息,其中不包含 notification
部分,像这样:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority":"high",
"data":{
"title" : "Custom notif",
"body" : "This is a custom data notification!",
"action" : "A|B|C"
}
}
}
收到此通知后,Firebase 不会将其显示在托盘面板中,而是将其传送到您的应用程序(您的应用程序在前台或后台)。 然后您可以使用 data
部分中的字段来创建和显示带有自定义操作的自定义通知。
您还可以将任何字段放入 data
部分,title
等字段只是示例。
客户端中的额外实施细节:
为了处理 android 中的数据消息(原生 and/or react native),你可以使用远程消息(但方式不同)。
反应本机:
对于本机反应中仅处理数据的通知,您可以使用此 example.
原生 android:
在本机 android 中,您可以使用服务的 onMessageReceived(RemoteMessage remoteMessage)
方法(实现 FirebaseMessagingService)。然后按照 remoteMessage.getData() 使用负载数据 here.
我有一个 android 应用程序。对于通知,我们必须显示一些操作按钮。当应用程序打开时,我们可以自由构建通知并显示操作按钮。
但是当应用程序关闭时,通知会在 android 的通知托盘中收到。应用程序开发人员无法控制 build ui 和操作按钮。我们现在如何显示按钮? 如何从服务器端格式化数据,以便在应用程序接收到数据时,我们可以看到操作按钮
根据我的经验,发生这种情况是因为您正在发送 通知消息。正如 google 所解释的 here 清楚:
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 云消息传递发送通知并始终以您自己的自定义方式显示收到的通知,您可以使用 FCM 数据消息,其中不包含 notification
部分,像这样:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority":"high",
"data":{
"title" : "Custom notif",
"body" : "This is a custom data notification!",
"action" : "A|B|C"
}
}
}
收到此通知后,Firebase 不会将其显示在托盘面板中,而是将其传送到您的应用程序(您的应用程序在前台或后台)。 然后您可以使用 data
部分中的字段来创建和显示带有自定义操作的自定义通知。
您还可以将任何字段放入 data
部分,title
等字段只是示例。
客户端中的额外实施细节:
为了处理 android 中的数据消息(原生 and/or react native),你可以使用远程消息(但方式不同)。
反应本机: 对于本机反应中仅处理数据的通知,您可以使用此 example.
原生 android:
在本机 android 中,您可以使用服务的 onMessageReceived(RemoteMessage remoteMessage)
方法(实现 FirebaseMessagingService)。然后按照 remoteMessage.getData() 使用负载数据 here.