当应用程序为 killed/closed 时,Expo 通知不会触发方法
Expo notification doesnt fire method when app is killed/closed
当应用程序处于 运行 或在后台但应用程序已关闭时,我使用 React Native Expo 和推送通知工作正常。它不调用处理通知的方法。我需要重定向到详细信息页面。我尝试使用函数组件和 class 组件,尝试迁移到旧通知和新通知。
import React, {useState, useEffect, useRef} from 'react';
import {
Image, ScrollView,
StyleSheet, Text, TouchableOpacity, Platform,
View, Linking,
} from 'react-native';
import * as Notifications from "expo-notifications";
const HomeScreen = (props) => {
useEffect(() => {
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
const {request, date} = notification ||{}
const {content} = request ||{}
const {data} = content ||{}
const {annKey,type} = data ||{}
if(annKey) {
// navigation.navigate('Detail', {annKey, updateFeed: true, onSelect},)
}
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
const {notification} = response ||{}
console.log(notification);
const {request, date} = notification ||{}
const {content} = request ||{}
const {data} = content ||{}
const {annKey, type} = data ||{}
if(annKey){
navigation.navigate('Detail', {annKey, updateFeed: true, onSelect},)
}
});
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
}, []);
}
export default HomeScreen;
问题是 useEffect()
在应用程序完成初始化后被调用得太晚了。因此在系统放弃通知之前不会添加监听器,也不会调用处理程序。
幸运的是,由于您使用的是新的 expo-notifications
库,它引入了 useLastNotificationResponse()
React 挂钩。它可以替换 addNotificationResponseReceivedListener()
侦听器和 returns 用户与之交互的最后一个通知(即点击)。它可以安全地用于 useEffect()
钩子。
这里是如何使用它(最好在你的根组件上实现它):
import * as Notifications from 'expo-notifications';
export default function App() {
const lastNotificationResponse = Notifications.useLastNotificationResponse();
React.useEffect(() => {
if (
lastNotificationResponse &&
lastNotificationResponse.notification.request.content.data['someDataToCheck'] &&
lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
) {
// navigate to your desired screen
}
}, [lastNotificationResponse]);
return (
/*
* your app
*/
);
}
您必须将此添加到您的 app.json 文件中:
"android": {
"useNextNotificationsApi": true,
},
当应用程序处于 运行 或在后台但应用程序已关闭时,我使用 React Native Expo 和推送通知工作正常。它不调用处理通知的方法。我需要重定向到详细信息页面。我尝试使用函数组件和 class 组件,尝试迁移到旧通知和新通知。
import React, {useState, useEffect, useRef} from 'react';
import {
Image, ScrollView,
StyleSheet, Text, TouchableOpacity, Platform,
View, Linking,
} from 'react-native';
import * as Notifications from "expo-notifications";
const HomeScreen = (props) => {
useEffect(() => {
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
const {request, date} = notification ||{}
const {content} = request ||{}
const {data} = content ||{}
const {annKey,type} = data ||{}
if(annKey) {
// navigation.navigate('Detail', {annKey, updateFeed: true, onSelect},)
}
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
const {notification} = response ||{}
console.log(notification);
const {request, date} = notification ||{}
const {content} = request ||{}
const {data} = content ||{}
const {annKey, type} = data ||{}
if(annKey){
navigation.navigate('Detail', {annKey, updateFeed: true, onSelect},)
}
});
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
}, []);
}
export default HomeScreen;
问题是 useEffect()
在应用程序完成初始化后被调用得太晚了。因此在系统放弃通知之前不会添加监听器,也不会调用处理程序。
幸运的是,由于您使用的是新的 expo-notifications
库,它引入了 useLastNotificationResponse()
React 挂钩。它可以替换 addNotificationResponseReceivedListener()
侦听器和 returns 用户与之交互的最后一个通知(即点击)。它可以安全地用于 useEffect()
钩子。
这里是如何使用它(最好在你的根组件上实现它):
import * as Notifications from 'expo-notifications';
export default function App() {
const lastNotificationResponse = Notifications.useLastNotificationResponse();
React.useEffect(() => {
if (
lastNotificationResponse &&
lastNotificationResponse.notification.request.content.data['someDataToCheck'] &&
lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
) {
// navigate to your desired screen
}
}, [lastNotificationResponse]);
return (
/*
* your app
*/
);
}
您必须将此添加到您的 app.json 文件中:
"android": {
"useNextNotificationsApi": true,
},