一年前,我成功地在我的反应本地应用程序中获得了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呢?
发布于 2021-10-14 11:15:07
查看setNotificationOpenedHandler或setNotificationWillShowInForegroundHandler的通知对象。
https://stackoverflow.com/questions/69048825
复制相似问题