React-native-onesignal: registerForPushNotifications should return a promise

Created on 28 Jun 2016  路  8Comments  路  Source: OneSignal/react-native-onesignal

Please enhance registerForPushNotifications so that it will return a promise mimicking: requestPermissions

From the doc it says:

This method returns a promise that will resolve when the user accepts, rejects, or if the permissions were previously rejected. The promise resolves to the current state of the permission.

I'm searching a way to know whether the user has accepted the notification permission or not.

Help Wanted

Most helpful comment

Would really like to see Promise functionality for registerForPushNotifications. Anyone? 馃檹馃徏

All 8 comments

More than happy to accept PR for this.

@jkasten2 can you help on this?

OneSignal SDK does not allow me to do that. Also, For my best understanding, there is no way to actually know if the user accepted the push notifications dialog or not. But, it's possible to check if the notifications are enabled at a later time.
Current work on progress:

+ (BOOL)isPushNotificationsEnabled {
    BOOL isEnabled = NO;
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
            isEnabled = NO;
        } else {
            isEnabled = YES;
        }
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert) {
            isEnabled = YES;
        } else{
            isEnabled = NO;
        }
    }
    return isEnabled;
}

When you'll ask for OneSignal.isPushNotificationsEnabled it should give you an answer if notifications are enabled or not. I'm still working on it so i'm not sure it's the proper way to do that.

Well, I took some inspiration from PushNotificationsIOS and now https://github.com/geektimecoil/react-native-onesignal/commit/0b8e7e0a6a568edf569a4cff07f094ba5c1250e3 fixes that. You can't return a promise right now on the request permissions method, but you can check what permissions your device have, with a callback.

Thanks!

@avishayil so there is no way to know when the user has chosen Allow or Don't Allow?

I know we can check the current permissions with checkPermissions but how would we know _when_ to check if we can't tell when they've responded to the modal?

image

Aren't you guys just using the native code that PushNotificationIOS is doing? Can't you use what they are? Should I just call await PushNotificationIOS.requestPermissions() myself?

Also, it would be helpful to update checkPermissions to use a promise syntax instead of a callback. Right now we're promisifying it manually:

export function checkPermissions() {
    return new Promise(resolve => {
        OneSignal.checkPermissions(resolve);
    });
}

@dwilt you can use AppState and call OneSignal.checkPermissions again when app "active" state to get user's response

Would really like to see Promise functionality for registerForPushNotifications. Anyone? 馃檹馃徏

Was this page helpful?
0 / 5 - 0 ratings