zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.74k stars 2.05k forks source link

handler for PushNotification 'registrationError' event in android #1492

Open samaneh-kamalian opened 4 years ago

samaneh-kamalian commented 4 years ago

how can handle errors in registration?

Dallas62 commented 4 years ago

Hi @samaneh-kamalian You can use: https://github.com/react-native-community/push-notification-ios#addeventlistener

PushNotificationIOS.addEventListener('registrationError', handler);

Regards,

Dallas62 commented 4 years ago

I will add onRegistrationError in .configure in the next release.

samaneh-kamalian commented 4 years ago
PushNotificationIOS.addEventListener('registrationError', handler);

thanks for your reply. I used react-native-push-notification for Android. PushNotificationIOS is for IOS! How can use it for android?!

Dallas62 commented 4 years ago

Hi, As far as I now, there is not registrationError in Firebase for Android.

samaneh-kamalian commented 4 years ago

For a registration process I need FCM token. react-native-push-notification works in special way. configure method must call out of components life cycle, so I call it in index.js. So when internet connection of mobile is off and user open my app, it can not get FCM token and I dont know how get fcm token when internet connection turn on! what function I must use?

Dallas62 commented 4 years ago

Hi, Firebase doesn't provide such event in there SDK. If you want to retry the token generation, you can use PushNotification.requestPermissions(). Also, the current implementation of Firebase for Android triggered onRegister is triggered when a new token is generated. https://firebase.google.com/docs/cloud-messaging/android/client#monitor-token-generation

samaneh-kamalian commented 4 years ago

Thanks for your reply. I have two problems: 1- as source code said :

requestPermissions(
        permissions?: Array<"alert" | "badge" | "sound">
    ): Promise<PushNotificationPermissions>;

so this function return Promise but when use PushNotification.requestPermissions().then((token)=>{}) it returns error: TypeError: undefined is not an object( evaluating '_reactNativePushNotification.default.requestPermissions().then') .

2-when I test my app on android10, even if internet connection is on, onRegister does not call. I must colse and open my app several time until onRegister called. I do not have this problem on android 6 and 8.

Dallas62 commented 4 years ago

For the first issue, this is because this function doesn’t return a Promise.

For the second one, this is a Firebase issue: https://stackoverflow.com/questions/37964737/firebase-cloud-messaging-no-token-and-strange-notification-behavior

mildfuzz commented 3 years ago

Hey @Dallas62, are you saying the type definition for requestPermissions is wrong? Presumably only in android land as this works fine on iOS

Dallas62 commented 3 years ago

Hi @mildfuzz This repository doesn't supply any type definitions. If the external repository with type definition is wrong, feel free to find it and fix it.

Actually, there is no Promise returned on Android since there is no "permissions" for notifications on Android. If you think it's not the right way, PR are welcome 😉

Regards,

mildfuzz commented 3 years ago

Okay, thanks 😊 just wanted to be clear that I had understood correctly.