Christian
Christian

Reputation: 859

Can i detect if the user disables push notification globally on trigger.io?

I am using trigger.io to make an app that supports push notifications. This works fine. If I disable the push notification globally, the app will stop receive any push notification. Are there any commands in trigger.io that can determine if the user has, via settings, enabled or disabled their push notifications for my application.

Upvotes: 0

Views: 787

Answers (2)

Patrick Rudolph
Patrick Rudolph

Reputation: 2241

You can actually get the push setting from iOS, however you'd need to implement this as your own native module. I'm using this in production in Trigger.io-based apps:

+ (void)getPushStatus:(ForgeTask*)task {

    NSArray *pushStatus;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        UIUserNotificationType notificationTypes = settings.types;

        if (notificationTypes == UIUserNotificationTypeBadge) {
            pushStatus = [NSArray arrayWithObjects: @"badge", nil];
        } else if (notificationTypes == UIUserNotificationTypeAlert) {
            pushStatus = [NSArray arrayWithObjects: @"alert", nil];
        } else if (notificationTypes == UIUserNotificationTypeSound) {
            pushStatus = [NSArray arrayWithObjects: @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeAlert)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeAlert | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"alert", @"sound", nil];
        } else if (notificationTypes == (UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", @"sound", nil];
        } else {
            // notificationTypes == UIUserNotificationTypeNone
            pushStatus = [[NSArray alloc] init];
        }
    } else {
        UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

        if (notificationTypes == UIRemoteNotificationTypeBadge) {
            pushStatus = [NSArray arrayWithObjects: @"badge", nil];
        } else if (notificationTypes == UIRemoteNotificationTypeAlert) {
            pushStatus = [NSArray arrayWithObjects: @"alert", nil];
        } else if (notificationTypes == UIRemoteNotificationTypeSound) {
            pushStatus = [NSArray arrayWithObjects: @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"alert", @"sound", nil];
        } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
            pushStatus = [NSArray arrayWithObjects: @"badge", @"alert", @"sound", nil];
        } else {
            // notificationTypes == UIRemoteNotificationTypeNone
            pushStatus = [[NSArray alloc] init];
        }
    }

    [task success:pushStatus];
}

Don't be frightened by that pile of code. The first half is for >=iOS8, the second half for everything below. It basically tells you which type of notifications the user has activated for your app.

The return value will be an array consisting of "badge", "alert" and/or "sound".

If the user disabled push for your app, the array will be empty.


PS: I'm not good at Objective-C, as the code above could obviously be done better. However, it still works for me ;)


EDIT: I've just realized that you also asked about Android. My answer is only relevant for iOS. According to this post Android 4.1: How to check notifications are disabled for the application? it's not possible to get that information on Android.

Upvotes: 0

proxim0
proxim0

Reputation: 1538

You can use

forge.parse.push.subscribedChannels(success, error

This will return the list of channels they are subscribed to, if your channel is not there then they are not subscribed and in the callback do what you want ( subscribe etc )

Upvotes: 1

Related Questions