首页 > 解决方案 > Expo Push Notifications Service/API - 如何在使用 Push Notification 后将匿名用户定向到应用程序中的特定 webview 屏幕

问题描述

使用 Expo 推送通知服务,我目前向所有匿名用户发送推送通知。我收集并写入 Firebase 数据库的唯一数据是“ExponentPushToken”,它标识了使用 expo 服务发送通知的唯一设备。我一直在使用以下命令在终端中发送推送通知:

curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
  "to": [
    "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
    "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"
  ],
  "title": "Hello World!",
  "body": "Example text here!!",
  "sound": "default"
}'

现在,我认为这不是最灵活的发送通知方式,但它让我可以快速上手。我现在的目标是,当在他们的设备上接收到特定推送通知时,能够将与(单击/按下)特定推送通知交互的用户发送到应用程序内的特定“状态”(特定 URL 的 web 视图)......我已经阅读了大部分文档,但我相信其中一些内容有点超出我的理解能力,以了解实现这一点所必需的内容(例如设置监听器等)。想知道是否有人可以帮助我简化实施?即使它有点“笨拙”,我对任何事情都持开放态度!

标签: iosreact-nativeexpoexpo-notifications

解决方案


您可以使用以下工具测试发送推送:https ://expo.dev/notifications 您可以看到一个数据(JSON)字段,您可以在其中通过推送发送一些附加信息(例如:{“id”:3 ,“idcustomer”:35,“事件”:“foo”}

当您将此推送发送到已注册的令牌时,应用程序会通过以下方式捕获事件:

notificationListener.current = 
    Notifications.addNotificationReceivedListener(notification => {

        console.log("Foreground notification", notification)
        // setNotification(notification);  // Set state

    });

    // This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
    responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
        console.log("Interaction done", response);
    }); 

当推送到达前台时,我习惯于显示一个 Snackbar ( RN Paper Snackbar ) 以进行额外的交互。使用 {notification},您可以使用这些数据显示内容、导航或其他任何您想做的事情。

不要忘记包括:

import * as Notifications from 'expo-notifications';
const notificationListener = useRef();
const responseListener = useRef();

推荐阅读