globocom / react-native-ua

React Native module for Urban Airship
MIT License
37 stars 33 forks source link

Know when the notification permission was accepted #32

Open guilhermebruzzi opened 7 years ago

guilhermebruzzi commented 7 years ago

Hi @rlepinski ,

I will create a pull request on this project, that will enable the possibility to know on javascript when the user accepted the notification permission's dialog on iOS and Android 6+.

On iOS there is a didRegisterForRemoteNotificationsWithDeviceToken method that you can declare on AppDelegate.m and you will receive there if the user confirmed the notification permission's dialog.

On Android there is a onRequestPermissionsResult method of an Activity, that you can declare to know if a user confirmed a permission dialog that you called on a given Activity (https://developer.android.com/training/permissions/requesting.html#perm-check).

The problem is knowing how to get the permission's result on Android 6+, if is the Urban Airship SDK that controls this permission's request. There is some sort of callback that Urban Airship Android's SDK give, that I can use to know if a user accepted the notification permission's dialog? Or any other way to know that?

Thank you in advance for your time! :D

rlepinski commented 7 years ago

Starting with iOS 8 they separated APNS registration (device token) with notificaiton permissions. Its actually entirely possible to message a device using APNS before the user accepts permissions if they have background notification capabilities enabled. The actual way to check for the permission is to check for the app delegate application:didRegisterUserNotificationSettings on iOS 8-9, but iOS 10 it gets a bit trickier. They deprecated UIUserNotificationSettings and introduced NSUserNotificationCenter which has no delegate callbacks for when settings change. Instead you get the result when you attempt to register for settings. However we added a registration delegate method that we will call anytime we detect a change in authorized types for iOS 8-10: http://docs.urbanairship.com/reference/libraries/ios/latest/Protocols/UARegistrationDelegate.html#/c:objc(pl)UARegistrationDelegate(im)notificationAuthorizedOptionsDidChange: For iOS, I would use that to handle user acceptance in a backwards compatible way.

For Android, there is actually no permissions required to receive and post notifications :). The user can disable notifications in the application setting screen, which you can check with http://docs.urbanairship.com/reference/libraries/android/latest/reference/com/urbanairship/push/PushManager.html#isOptIn(). You can check if it changes whenever the app is resumed (they have to background the app to disable it).

guilhermebruzzi commented 7 years ago

Great @rlepinski, thank you! :D

mkko commented 7 years ago

So, @guilhermebruzzi, are you working on this? We'd need to have the UARegistrationDelegate exposed on the React Native side as well, so I'm interested to put some effort into this as well.

guilhermebruzzi commented 7 years ago

Hi @mkko, I've just sent a PR https://github.com/globocom/react-native-ua/pull/33 For our needs to work on iOS 9, iOS 10 and Android 4.1+ (which already have notification permission accepted), this solves the problem. Do you and @rlepinski think it's a good solution?