处理传入通知
编辑
学习如何响应应用收到的通知并根据事件采取行动。
expo-notifications 库包含事件监听器,用于处理您的应用在接收到通知时的响应方式。
通知事件监听器
addNotificationReceivedListener 和 addNotificationResponseReceivedListener 事件监听器在接收到通知或与其互动时接收一个对象。
这些监听器允许您在应用打开并处于前台时及应用处于后台或关闭状态并且用户点击通知时,添加通知接收时的行为。
useEffect(() => { registerForPushNotificationsAsync().then(token => setExpoPushToken(token)); notificationListener.current = Notifications.addNotificationReceivedListener(notification => { setNotification(notification); }); responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { console.log(response); }); return () => { Notifications.removeNotificationSubscription(notificationListener.current); Notifications.removeNotificationSubscription(responseListener.current); }; }, []);
Android 通知对象示例来自 addNotificationReceivedListener
使用 Notifications.addNotificationReceivedListener 时,回调函数接收到的 notification 对象示例:
// console.log(notification); { "request": { "trigger": { "remoteMessage": { "originalPriority": 2, "sentTime": 1724782348210, "notification": { "usesDefaultVibrateSettings": false, "color": null, "channelId": null, "visibility": null, "sound": null, "tag": null, "bodyLocalizationArgs": null, "imageUrl": null, "title": "Chat App", "ticker": null, "eventTime": null, "body": "New message from John Doe", "titleLocalizationKey": null, "notificationPriority": null, "icon": null, "usesDefaultLightSettings": false, "sticky": false, "link": null, "titleLocalizationArgs": null, "bodyLocalizationKey": null, "usesDefaultSound": false, "clickAction": null, "localOnly": false, "lightSettings": null, "notificationCount": null }, "data": { "channelId": "default", "message": "New message from John Doe", "title": "Chat App", "body": "{\"senderId\":\"user123\",\"senderName\":\"John Doe\",\"messageId\":\"msg789\",\"conversationId\":\"conversation-456\",\"messageType\":\"text\",\"timestamp\":1724766427}", "scopeKey": "@betoatexpo/expo-notifications-app", "experienceId": "@betoatexpo/expo-notifications-app", "projectId": "51092087-87a4-4b12-8008-145625477434" }, "to": null, "ttl": 0, "collapseKey": "dev.expo.notificationsapp", "messageType": null, "priority": 2, "from": "115310547649", "messageId": "0:1724782348220771%0f02879c0f02879c" }, "channelId": "default", "type": "push" }, "content": { "autoDismiss": true, "title": "Chat App", "badge": null, "sticky": false, "sound": "default", "body": "New message from John Doe", "subtitle": null, "data": { "senderId": "user123", "senderName": "John Doe", "messageId": "msg789", "conversationId": "conversation-456", "messageType": "text", "timestamp": 1724766427 } }, "identifier": "0:1724782348220771%0f02879c0f02879c" }, "date": 1724782348210 }
您可以通过记录 notification.request.content.data 对象直接访问通知自定义数据:
// console.log(notification.request.content.data); { "senderId": "user123", "senderName": "John Doe", "messageId": "msg789", "conversationId": "conversation-456", "messageType": "text", "timestamp": 1724766427 }
iOS 通知对象示例来自 addNotificationReceivedListener
使用 Notifications.addNotificationReceivedListener 时,回调函数接收到的 notification 对象示例:
// console.log(notification); { "request": { "trigger": { "class": "UNPushNotificationTrigger", "type": "push", "payload": { "experienceId": "@betoatexpo/expo-notifications-app", "projectId": "51092087-87a4-4b12-8008-145625477434", "scopeKey": "@betoatexpo/expo-notifications-app", "aps": { "thread-id": "", "category": "", "badge": 1, "alert": { "subtitle": "Hey there! How's your day going?", "title": "Chat App", "launch-image": "", "body": "New message from John Doe" }, "sound": "default" }, "body": { "messageId": "msg789", "timestamp": 1724766427, "messageType": "text", "senderId": "user123", "senderName": "John Doe", "conversationId": "conversation-456" } } }, "identifier": "3AEB849E-9059-4D09-BC3B-9A0B104CF062", "content": { "body": "New message from John Doe", "sound": "default", "launchImageName": "", "badge": 1, "subtitle": "Hey there! How's your day going?", "title": "Chat App", "data": { "conversationId": "conversation-456", "senderName": "John Doe", "senderId": "user123", "messageType": "text", "timestamp": 1724766427, "messageId": "msg789" }, "summaryArgument": null, "categoryIdentifier": "", "attachments": [], "interruptionLevel": "active", "threadIdentifier": "", "targetContentIdentifier": null, "summaryArgumentCount": 0 } }, "date": 1724798493.0589335 }
您可以通过记录 notification.request.content.data 对象直接访问通知自定义数据:
// console.log(notification.request.content.data); { "senderId": "user123", "senderName": "John Doe", "messageId": "msg789", "conversationId": "conversation-456", "messageType": "text", "timestamp": 1724766427 }
有关这些对象的更多信息,请参见 Notification 文档。
前台通知行为
要处理应用在 前台 接收到通知时的行为,请使用 Notifications.setNotificationHandler 和 handleNotification() 回调来设置以下选项:
shouldPlaySoundshouldSetBadgeshouldShowBannershouldShowList
Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldPlaySound: false, shouldSetBadge: false, shouldShowBanner: true, shouldShowList: true, }), });
关闭通知行为
在 Android 上,用户可以设置某些操作系统级别的设置,通常涉及性能和电池优化,这可能会在应用关闭时阻止通知的送达。例如,在使用 Android 9 及更低版本的 OnePlus 设备上有一个设置选项是 深度清理。