[英]iOS background notification handler issue using @react-native-firebase/messaging
當應用程序處於后台或關閉 state 時,我遇到了這個問題。 在所有情況下,通知都會顯示在通知中心,但messaging().setBackgroundMessageHandler
並非每次都被調用,而只是在對應用程序進行嚴格檢查時才調用幾次。 遵循雲消息傳遞的官方文檔,將@react-native-firebase/messaging
和@react-native-firebase/app
升級到版本 10.2.0 。 所有狀態回調在Android中都運行良好,並且每次都獲得完美,問題僅存在於iOS並且不確定是什么導致了所有這些混亂。
在此處為我正在使用的 ref 添加代碼。 在入口文件即index.js中
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log("Message handled in the background! 1", remoteMessage);
});
function HeadlessCheck({ isHeadless }) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}
return <App />;
}
AppRegistry.registerComponent(appName, () => HeadlessCheck);
在App.js 中:
const App = () => {
var notif = new NotifService();
useEffect(() => {
requestUserPermission()
checkPermission()
console.log("checkPermission")
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log("A new FCM message arrived!: foreground: ", remoteMessage)
console.log("A new FCM message arrived!: foreground: ", remoteMessage.messageId)
// Alert.alert('A new FCM message arrived!: foreground: ', JSON.stringify(remoteMessage));
notif.localNotif();
});
return unsubscribe;
}, []);
useEffect(() => {
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification,
);
Alert.alert('Message handled in the background state!', JSON.stringify(remoteMessage));
// navigation.navigate(remoteMessage.data.type);
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
Alert.alert('Message handled in the quit state!', JSON.stringify(remoteMessage));
// setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
}
// setLoading(false);
});
}, []);
async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
// 1
async function checkPermission() {
const enabled = await messaging().hasPermission();
console.log("checkPermission: enabled: ", enabled)
if (enabled) {
getToken();
} else {
requestPermission();
}
}
// 3
async function getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log('fcmToken @@1:', fcmToken);
if (!fcmToken) {
fcmToken = await messaging().getToken();
console.log('fcmToken @@2:', fcmToken);
if (fcmToken) {
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
}
// 2
async function requestPermission() {
console.log("requestPermission:")
try {
await messaging().requestPermission();
// User has authorised
getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
}
我使用 firebase rest API 測試的有效負載:
{ "to": "TOKEN_HERE", "data": { "notification": { "title": "ABC1", "body": "xyz1" }, "Name": "121" }, "notification": { "title": "ABC1", "body": "xyz1" }, "priority": "high", "content_available": true }
如果有人遇到同樣的問題並解決了,請在此處添加。 現在堅持了好幾天。 謝謝。
Version 7.18.0 - August 13, 2020 Cloud Messaging
Deprecated setBackgroundMessageHandler. Use the new API onBackgroundMessage instead.
https://firebase.google.com/support/release-notes/js#cloud-messaging_2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.