当应用程序处于后台或关闭状态时,我遇到这个问题。在所有情况下,通知都会显示在通知中心,但
messaging().setBackgroundMessageHandler
并不是每次都会被调用,而是在应用程序上严格检查时仅被调用几次。遵循云消息传递官方文档,将@react-native-firebase/messaging
和@react-native-firebase/app
升级到版本10.2.0。所有状态回调在Android中都工作正常,并且每次都完美获得,问题仅存在于iOS,并且不确定是什么导致了所有这些混乱。
在此处添加我正在使用的参考代码。 在入口文件中,即 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 测试的Payload:
{ "to": "TOKEN_HERE", "data": { "notification": { "title": "ABC1", "body": "xyz1" }, "Name": "121" }, "notification": { "title": "ABC1", "body": "xyz1" }, "priority": "high", "content_available": true }
如果有人遇到同样的问题并解决了,请在此处添加。现在已经被这个问题困住好几天了!谢谢。
使用@react-native-firebase/messaging的iOS后台通知处理程序问题
我在 IOS 中也面临这个问题需要帮助
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