我正在使用 @ react-native-firebase 处理推送通知。问题是当应用程序在后台运行时它正在工作。如果应用程序处于前台或打开状态,则推送通知不起作用。另外,我正在使用 notifee 创建自定义通知。它也不起作用。
这是版本详细信息。
"@react-native-firebase/app": "^8.3.1",
"@react-native-firebase/messaging": "^7.7.2",
"@notifee/react-native": "^0.12.3",
实施代码
componentDidMount() {
console.log('componentDidMount App ');
// AppState.addEventListener('change', this._handleAppStateChange);
messaging()
.getInitialNotification()
.then(remoteMessage => {
console.log('Notification caused app to open from quit state:');
if (remoteMessage) {
console.log(remoteMessage.notification);
}
});
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!');
console.log(remoteMessage);
});
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!');
console.log(remoteMessage);
});
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('Notification caused app to open from background state:');
console.log(remoteMessage);
Actions.AddMeeting({
actionType: 'add',
});
// navigation.navigate(remoteMessage.data.type);
// });
messaging().registerDeviceForRemoteMessages();
notifee.registerForegroundService(() => {
return new Promise(resolve => {
// Long running task...
});
});
const channelId = notifee.createChannel({
id: 'default',
name: 'Default Channel',
});
async function onMessageReceived(message) {
// Do something
// notifee.displayNotification(message.data.msg);
console.log('onMessageReceived');
console.log(JSON.stringify(message));
notifee.displayNotification({
title: 'Foreground service',
body: 'This notification will exist for the lifetime of the service runner',
android: {
channelId,
asForegroundService: true,
color: '#dc193c',
colorized: true,
},
});
}
messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);
}
答案 0 :(得分:0)
messaging().onNotificationOpenedApp(remoteMessage => {
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification,
);
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,
);
setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
}
setLoading(false);
});
使用此通知侦听器