react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

GetToken Method

Open thomaschaaf opened this issue 4 years ago • 4 comments

Feature Request

A method to get the token.

Why it is needed

It would be easier to use the package. Currently you have to save the token from the configure method.

Code sample

PushNotification.getToken(function (token) {
  console.log(token);
});

thomaschaaf avatar Aug 30 '20 07:08 thomaschaaf

Is there any decisions/progress on this one?

Because we hit a problem with iOS real devices, where we ask for the system permissions later in the app (when we present a custom dialog), however it seems that the token at that point of time is not generated and stays empty. We use the following code:

Notifications.configure({ onRegister: async ({ token }) => { await AsyncStorage.setItem("token", token); }, onNotification: (notification: PushNotification) => { notification.finish(PushNotificationIOS.FetchResult.NoData); }, onRegistrationError: (err) => { // log exception }, permissions: { alert: true, badge: true, sound: true }, popInitialNotification: true, requestPermissions: false });

Could please someone advise what we should do/change to avoid this issues?

defigor avatar Jan 22 '21 23:01 defigor

+1

sophie-li avatar Jun 04 '21 18:06 sophie-li

AsyncStorage

This is how I did mine and seems to work, the only thing I wish could get done is re-creating token on account login or such but I have no idea how to.

onRegister: async token => { if (token) { await AsyncStorage.setItem( @${COMPANY_TREE}/notificationToken, JSON.stringify(token), err => { if (err) { console.log('PushNotification onRegister error:', err); } else { console.log('PushNotification onRegister saved!', token); } }, ).catch(err => { console.log('error is: ' + err); console.log('PushNotification onRegister error:', err); }); } },

abdullahIsa avatar Dec 12 '21 05:12 abdullahIsa

+1

florinrelea avatar May 03 '22 15:05 florinrelea