![](/img/trans.png)
[英]How to send local push notification with react-native-onesignal
[英]How to get pushToken from react-native-onesignal?
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?
謝謝傑里米 :)
我在文檔中找到了這個:
const deviceState = await OneSignal.getDeviceState();
所以我認為它可能是這樣的: const deviceState = (await OneSignal.getDeviceState()).pushToken;
基於此鏈接: https : //documentation.onesignal.com/v7.0/docs/react-native-sdk
查看setNotificationOpenedHandler
或setNotificationWillShowInForegroundHandler
的通知對象內部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.