首次安装应用程序时未收到推送通知

Har*_*n V 5 firebase-cloud-messaging

当我第一次安装该应用程序时,没有收到推送通知,如果我终止该应用程序并再次打开,推送通知工作正常。当我控制台代码并通过 FCM 手动触发推送通知时,我发现 onMessage 函数没有接收通知数据,而 getInitialNotification() 结果显示为未定义。

请检查以下代码以获取推送通知以及我使用的软件包和版本。

软件包:“@react-native-firebase/messaging”:“^7.5.0”,“@react-native-community/push-notification-ios”:“^1.10.1”,“react-native-push-notification” ": "^8.1.1",

代码 :

componentWillUnmount() {
if (Platform.OS === "ios" && this.messageListener1 && this.messageListener2) {
this.messageListener1();
this.messageListener2();
}
}
async requestUserPermission() {
const { navigate } = { ...this.props }
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
this.messageListener1 = messaging().onMessage(async remoteMessage => {
PushNotification.localNotification({
/* Android Only Properties */
id: remoteMessage.data.id, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
autoCancel: true, // (optional) default: true
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
group: remoteMessage.data,
userInfo: {
data: remoteMessage.data,
},
title: remoteMessage.notification.title,
message: remoteMessage.notification.body,
});
});
messaging()
.getInitialNotification()
.then(async remoteMessage => {
if (remoteMessage) {
if (await GlobalStorage.handleInitialNotification(remoteMessage)) {
navigate(remoteMessage);
PushNotification.cancelLocalNotifications({ id: remoteMessage.data.id });
}
}
});
this.messageListener2 = messaging().onNotificationOpenedApp(remoteMessage => {
GlobalStorage.handleInitialNotification(remoteMessage)
navigate(remoteMessage);
PushNotification.cancelLocalNotifications({ id: remoteMessage.data.id });
});
Run Code Online (Sandbox Code Playgroud)

注意:获取 FCM 令牌没有问题。

Har*_*n V 0

该问题仅主要在 Android 设备上遇到,这是我用来解决该问题的解决方案

 const { navigate,
            } = { ...this.props }
            PushNotification.configure({
                onRegister: function (token) {
                   
                },
                onNotification: function (notification) {
                    const clicked = notification.userInteraction ? notification.userInteraction : "";
                    notification.finish(PushNotificationIOS.FetchResult.NoData);
                    if (clicked) {
                        navigate(notification);s
                        PushNotification.cancelLocalNotifications({ id: notification.id });
                    } else if (!notification.foreground) {
                        navigate(notification);
                        PushNotification.cancelLocalNotifications({ id: notification.id });
                    }
                    else {
                        PushNotification.localNotification({
                            /* Android Only Properties */
                            channelId: "app_name", // (required)
                            channelName: "app_name", // (required)
                            id: notification.id, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
                            // autoCancel: true, // (optional) default: true
                            vibrate: true, // (optional) default: true
                            vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
                            group: notification.data, // (optional) add group to message
                            priority: "high", // (optional) set notification priority, default: high
                            visibility: "private", // (optional) set notification visibility, default: private
                            importance: "high",
                            userInfo: {
                                id: notification.id,
                                name: "name",
                                title: notification.title,
                                message: notification.message,
                                data: notification.data

                            },
                            title: notification.title,
                            message: notification.message,
                        });
                    }
                    notification.finish(PushNotificationIOS.FetchResult.NoData);
                },
                senderID: "",
                permissions: {
                    alert: true,
                    badge: true,
                    sound: true
                },
                popInitialNotification: true,
                requestPermissions: true
            });
Run Code Online (Sandbox Code Playgroud)