Saturday, May 25, 2013

UIApplication registerForRemoteNotificationTypes example in Objective C (iOS)


[UIApplication registerForRemoteNotificationTypes]

Register to receive notifications of the specified types from a provider via Apple Push Service.
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
Parameters
types
A bit mask specifying the types of notifications the application accepts. SeeUIRemoteNotificationType for valid bit-mask values.
Discussion of [UIApplication registerForRemoteNotificationTypes]
When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken:method; if registration doesn’t succeed, the delegate is informed via theapplication:didFailToRegisterForRemoteNotificationsWithError:method. If the application delegate receives a device token, it should connect with its provider and pass it the token.[UIApplication registerForRemoteNotificationTypes]
iOS does not display or play notification types specified in the notification payload that are not one of the requested ones. For example, if alert messages are not one of the accepted notification types, iOS does not display an alert even if one is specified in the notification payload. To find out what the application’s current notification types are, call the enabledRemoteNotificationTypes method.
Example of [UIApplication registerForRemoteNotificationTypes]
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //generic setup and whatnot

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: remoteNotifTypes];
    if (([[NSUserDefaults standardUserDefaults] stringForKey: deviceTokenKey]) &&
        ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != remoteNotifTypes))
    {
        //user has probably disabled push. react accordingly.
    }
}
Example of [UIApplication registerForRemoteNotificationTypes]
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Example of [UIApplication registerForRemoteNotificationTypes]
 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 UIRemoteNotificationTypeBadge | 
 UIRemoteNotificationTypeAlert | 
 UIRemoteNotificationTypeSound];