3

I'm trying to determine whether or not the user enabled push notification for my app using phonegap (ver 1.7).

So far I only came up with this plugin which doesn't have a method specifically to check that issue, but does have a method to register push notification, which may return an error, which may be an indication to the user disabling push notification. But as you can see, that's very vague.

So my question is - is there a plugin (or any other way) to determine if the user enabled push notification for my app?

Oren A
  • 5,870
  • 6
  • 43
  • 64

1 Answers1

4

You can check if any type of push notifications are enabled by using this :

if([UIApplication sharedApplication].enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)

this status can be accesd by call a simple plugin after device ready.

UPDATE :Write a plugin and inside that plugin please check the status and return to javascript

if you need to save it on server first thing you need one server side api and some native side code, after registering push notification on ios there are two callback functions

  1. didFailToRegisterForRemoteNotificationsWithError
  2. didRegisterForRemoteNotificationsWithDeviceToken

    so if success you can send a request to your server with the device id or any unique ID so you can save the registered devices.

    • (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

      NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"< "withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString: @" " withString: @""];

      [self postUpdateRequest:token]; // request to your server

      }

for sending POST request use

 NSURL *aUrl = [NSURL URLWithString:uRI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
   NSString *postString =@"Your Data";
     [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

   NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
                                                               delegate:self];
  [connection start];
        if(connection) {
       // success
        } else {
       //error
        }

if registration was failed (didFailToRegisterForRemoteNotificationsWithError) you can send a requset to server with error data OR Not.

from phonegap side after device ready send one ajax request to you server and check whether your device is registerted or not. for getting unique id you may use simple plugin. for writing simple plugin please follow this http://docs.phonegap.com/en/edge/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide

Unfortunately you can't enable or disable the Push Notifications for your app from the app code. check this

Community
  • 1
  • 1
Arjun T Raj
  • 3,187
  • 1
  • 21
  • 44
  • Thanks for the replay, but my question is: how do I know the user ticked the "allow push notification" on his iPhone for my app. This doesn't seem to answer it (unless I'm missing something). – Oren A Aug 28 '13 at 11:35
  • I don't want to enable or disable the Push Notifications for my app from the app code. I want to know what the user chose for my app. If he enabled or disabled push notifications for my app. – Oren A Aug 28 '13 at 12:25
  • @OrenA check the edit "You can check if any type of push notifications are enabled :" if not enabled means user disabled ! – Arjun T Raj Aug 28 '13 at 12:31
  • But this is native code, and i want to do it with phonegap! As the title reads: Checking if user enabled push notification on iPhone using *phonegap* 1.7 – Oren A Aug 28 '13 at 12:41
  • in phonegap we can write plugins just write a plugin and inside the plugin check the status .http://docs.phonegap.com/en/edge/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide – Arjun T Raj Aug 28 '13 at 12:46