简体   繁体   English

向 Firebase 通知添加操作

[英]Add Action To Firebase Notification

I'm trying to add an action button to notifications produced on firebase 9.0.0, for Android, when app is in background .我正在尝试为 Android 的 firebase 9.0.0 上生成的通知添加一个操作按钮,当应用程序处于后台时

Any ideas?有什么想法吗?

Thanks!!谢谢!!

firebase-cloud-messaging doesn't provide an API to add action buttons to the notifications. firebase-cloud-messaging 不提供 API 来向通知添加操作按钮。

You can request new features to the firebase library here: https://firebase.google.com/support/contact/bugs-features/您可以在此处向 firebase 库请求新功能: https : //firebase.google.com/support/contact/bugs-features/

For now what you can do is to send a data-message with a custom payload via the server-side API You can then receive that payload in onMessageReceived() and generate your custom notification.现在您可以做的是通过服务器端 API data-message带有自定义负载的data-message ,然后您可以在 onMessageReceived() 中接收该负载并生成您的自定义通知。

So in order to customize notifications when app is on background, as Diego mentioned, the only current way is to create the notification yourself.因此,为了在应用程序处于后台时自定义通知,正如 Diego 提到的,目前唯一的方法是自己创建通知。 Adding "data" key to notification payload, results in onMessageReceived() callback, on which you can create any notification andy notify.将“数据”键添加到通知有效负载,会导致onMessageReceived()回调,您可以在该回调上创建任何通知和通知。

Thing is, I tried to send a notification from Firebase console, rather then from the API.问题是,我尝试从 Firebase 控制台发送通知,而不是从 API 发送通知。 There I couldn't add the data key properly and catch it.在那里我无法正确添加数据键并捕获它。 From API all works fine.从 API 一切正常。

I would consider reviewing the following documentation:我会考虑查看以下文档:

Handle messages in a backgrounded app在后台应用程序中处理消息

https://firebase.google.com/docs/cloud-messaging/downstream#backgrounded https://firebase.google.com/docs/cloud-messaging/downstream#backgrounded

This looks like you would need to change the intent filter for your FirebaseMessagingService to handle the OnClick action.这看起来您需要更改 FirebaseMessagingService 的 Intent 过滤器来处理 OnClick 操作。

Assuming you already know that you have to use only data key ( I answered here, too ) to send to your application in the background, you can add an action to notification message builder like this:假设您已经知道您必须使用数据密钥我也在这里回答)在后台发送到您的应用程序,您可以向通知消息生成器添加一个操作,如下所示:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.addAction(
                    R.drawable.ic_setting_light,
                    res.getString(**R.string.Your_Button_String**),
                    PendingIntent.getActivity(
                            context,
                            0,
                            **Your_Intent_To_Open_When_Button_Is_Click**,
                            PendingIntent.FLAG_UPDATE_CURRENT));

Of course this has to be inside handling logic from Android side.当然,这必须在 Android 端的内部处理逻辑中。

Note: Notification actions are supported only in Android 4.1 or later.注意:通知操作仅在 Android 4.1 或更高版本中受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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