Tuesday, October 28, 2014

iOS 8.0 Registering, Scheduling, and handling User notifications


Biggest change in iOS 8.0 is that application needs to register the type of notifications application indent to deliver. The system then gives ability for the user the ability to limit the types of notification the app displays. 
A new method is available registerUserNotificaitonSettings method of UIApplication. If app don’t register any notification types, the system pushes all remote notifications to the app silently, that is without displaying any user interface. Or the app can explicitly declare that it will not display any user interface for notifications. 

Below code displays how to register for Notifications 

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificaitonTypeAlert | UIUserNotificationTypeSound; 
UIUSerNotificationSEttings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[UIApplication sharedApplication]registerUserNotificationSettings:mySettings]; 

Passing category type is important in case of handling specific actions for notifications, in this case, it is specified as nil and custom actions won’t be available for the notification. 
When the registerUSerNotificationSettings api is called, application presents a dialog to the user for permission to present the type of notifications the app registered. After the user replies iOS call-back to the UIApplicationDelegate object with the didRegisterUserNotificationSettings method passing a UIUserNotificationSettingsType object that specifies the type of notifications the user allows. Application also required to check the current notification settings by calling currentUSerNotificationSettings. From iOS 8.0 and later registerUserNotificationSettings applies to both local and remote Notificaitons and this deprecates the prior version methods registerForRemoteNotificaitonType. 

Registering for Remote notifications 
The remote notification registration is similar to the pre-iOS 8.0 versions 

1. Register remote notification types the app supports using registerUserNotificaitonSettings
2. Register to receive the APNs token using registerForeRemoteNotifications 
3. Store the device token returned upon success registration, handle the errors gracefully
4. Forward the push token to the app’s push provider 

Handling local and remote notifications 
Handling of notification is same as earlier versions of iOS that is, 

- If app was not running in the foreground. In this case, OS will take care of presenting this notification to the user. 
- When user tap on the notification or action buttons on the notification alert, app will be called with didFinishLaunchingWithOptions and the payload is passed in as argument

- When app was running in the foreground while receiving the notification, didReceiveRemote/LocalNotification method will be called. 

references:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

No comments:

Post a Comment