简体   繁体   English

带有2个按钮的Android推送通知用于渐进式Web应用程序?

[英]Android push notifications with 2 buttons for Progressive Web Apps?

I'm building an offline/installable Progressive Web App (PWA) with a Firebase Cloud Messaging (FCM) backend. 我正在使用Firebase云消息传递(FCM)后端构建脱机/可安装的渐进式Web应用程序(PWA)。 Is it possible to generate an Android push notification with 2 buttons for an installed PWA? 是否可以为安装的PWA生成带有2个按钮的Android推送通知? Here is an example: 这是一个例子: 在此处输入图片说明

Yes, this is possible over the new FCM REST API: 是的,这可以通过新的FCM REST API实现:

https://firebase.googleblog.com/2018/05/web-notifications-api-support-now.html https://firebase.googleblog.com/2018/05/web-notifications-api-support-now.html

Payload Example: 有效负载示例:

    {
        "message": {
            "webpush": {
                "notification": {
                    "title": "Fish Photos 🐟",
                    "body":
                        "Thanks for signing up for Fish Photos! You now will receive fun daily photos of fish!",
                    "icon": "firebase-logo.png",
                    "image": "guppies.jpg",
                    "data": {
                        "notificationType": "fishPhoto",
                        "photoId": "123456"
                    },
                    "click_action": "https://example.com/fish_photos",
                    "actions": [
                        {
                            "title": "Like",
                            "action": "like",
                            "icon": "icons/heart.png"
                        },
                        {
                            "title": "Unsubscribe",
                            "action": "unsubscribe",
                            "icon": "icons/cross.png"
                        }
                    ]
                }
            },
            "token": "<APP_INSTANCE_REGISTRATION_TOKEN>"
        }
    }

Note that you will need to register callbacks for these custom actions in your service worker code, to handle whatever is supposed to happen when the user clicks on a custom button: 请注意,您将需要在服务工作者代码中注册这些自定义操作的回调,以处理用户单击自定义按钮时应该发生的一切:

Service Worker: 服务人员:

    // Retrieve an instance of Firebase Messaging so that it can handle background messages.
    const messaging = firebase.messaging();

    // Add an event listener to handle notification clicks
    self.addEventListener('notificationclick', function(event) {
         if (event.action === 'like') {
                 // Like button was clicked

                 const photoId = event.notification.data.photoId;
                 like(photoId);
         }
         else if (event.action === 'unsubscribe') {
                 // Unsubscribe button was clicked

                 const notificationType = event.notification.data.notificationType;
                 unsubscribe(notificationType);
         }

         event.notification.close();
    });

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

相关问题 在 iOS 中为渐进式 Web 应用推送通知 - Push Notifications in iOS for Progressive Web Apps 渐进式 web 应用程序 (PWA) 无法在 android 手机的屏幕上弹出推送通知 - Progressive web app (PWA) not able to make push notifications pop up on screen of android phone 有没有办法使用渐进式网络应用程序 (PWA) 添加徽章通知? - Is there a way to add badge notifications using progressive web apps (PWA)? gcm推送通知,渐进式webapp - gcm push notifications, progressive webapp 接收在 WebView 中运行的 Web 应用程序的推送通知 - Receive Push Notifications for Web Apps running in a WebView Web套接字和推送通知android - Web socket and push notifications android 可以渐进式 web 应用程序访问 android 文件系统 - Can progressive web apps access android file system Android 上的 Microsoft Edge 中的 Web 推送通知不起作用 - Web push notifications in Microsoft Edge on Android not working 从 Web 服务器推送通知到 android 应用程序 - Push notifications from Web server to android application 渐进式Web应用程序的局限性:将本机应用程序转换为渐进式Web应用程序 - Limits of Progressive Web Apps: transforming a native app into a progressive web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM