0

1 年前,我在我的 react-native 应用程序中成功获得了 pushToken。但是现在官网的示例代码已经改了,所以我找不到任何如何获取pushToken的代码。

这是我去年使用的代码。(类组件)

constructor(props) {
    super(props);
    ...
    OneSignal.addEventListener('ids', this.onIds);
}
componentWillUnmount() {
    OneSignal.removeEventListener('received', this.onReceived);
    OneSignal.removeEventListener('opened', this.onOpened);
    OneSignal.removeEventListener('ids', this.onIds);
}
onIds(device) {
    console.log('Device info: ', device);  // I got pushToken here successfully last year.
}

这是我当前的代码。(功能组件)

useEffect(() => {
    // OneSignal Init Code
    OneSignal.setAppId("My-OneSignal-Key");
    OneSignal.setLogLevel(6, 0);
    // END OneSignal Init Code

    // Prompt for push on iOS
    OneSignal.promptForPushNotificationsWithUserResponse(response => {
        console.log("Prompt response:", response);
    });

    // Method for handling notifications received while app in foreground
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
        console.log("OneSignal: notification will show in foreground:", notificationReceivedEvent);
        let notification = notificationReceivedEvent.getNotification();
        console.log("notification: ", notification);
        const data = notification.additionalData
        console.log("additionalData: ", data);
        // Complete with null means don't show a notification.
        notificationReceivedEvent.complete(notification);
    });

    // Method for handling notifications opened
    OneSignal.setNotificationOpenedHandler(notification => {
        console.log("OneSignal: notification opened:", notification);
    });
}

但是现在,我应该从哪里获得 pushToken?

4

2 回答 2

1

我在文档中找到了这个:

const deviceState = await OneSignal.getDeviceState();

所以我认为它可能是这样的:

const deviceState = (await OneSignal.getDeviceState()).pushToken;
于 2021-10-17T01:23:31.143 回答
-1

setNotificationOpenedHandler查看or的通知对象内部setNotificationWillShowInForegroundHandler

于 2021-10-14T11:15:07.770 回答