Friday 29 January 2016

Push notification

Push notifications:
There are two types of notifications in ios, local and remote notification. Remote notifications are also called push notification. Notifications enable an app that isn’t running in the foreground to let its users know it has information for them. The information could be a message, an impending calendar event, or new data on a remote server. When presented by the operating system, local and remote user notifications look and sound the same. They can display an alert message or they can badge the app icon. They can also play a sound when the alert or badge number is shown.
1) An app enables push notifications. The user has to confirm that he wishes to receive these notifications.
2) The app receives a “device token”. You can think of the device token as the address that push notifications will be sent to.
3) The app sends the device token to your server.
4) When something of interest to your app happens, the server sends a push notification to the Apple Push Notification Service, or APNS for short.
5) APNS sends the push notification to the user’s device.
6) When the user’s device receives the push notification, it shows an alert, plays a sound and/or updates the app’s icon. The user can launch the app from the alert. The app is given the contents of the push notification and can handle it as it sees fit. 
If you want to notify the users of your app about external events while the app is closed, you still need push notifications.
To enable push notification in an app you need some dependencies:
1)To enable push notifications in your app, it needs to be signed with a provisioning profile that is configured for push.
2)your server needs to sign its communications to APNS with an SSL certificate.
3)Each push app needs its own unique ID because push notifications are sent to a specific application. (You cannot use a wildcard ID.)
Register for remote notification in app delegate.m-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Let the device know we want to receive push notifications

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:

(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}

The new call to registerForRemoteNotificationTypes: tells the OS that this app wants to receive push notifications.
There is one more thing you need to add in order to be able to receive push notifications. Add the following to AppDelegate.m:
To obtain deviceToken:- 
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
 NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
When your app registers for remote (push) notifications, it tries to obtain a “device token”. This is a 32-byte number that uniquely identifies your device. 
The didRegisterForRemoteNotificationsWithDeviceToken method is usually called right away after you’ve registered for push notifications. Usually, but not always. 

Receiving Push Messages in the App
So what happens when your iPhone receives a push notification? There are three possible situations:
The app is currently running in the foreground.
The app is closed, either the iPhone’s home screen is active or some other app is running.
The iPhone is locked.
The final modifications to our app all take place in AppDelegate.m, as the application delegate is the one who receives the push notifications. There are two places for this:
application:didFinishLaunchingWithOptions:. If your app wasn’t running when the notification came in, it is launched and the notification is passed as part of the launchOptions dictionary.
application:didReceiveRemoteNotification:. This method is invoked if your app is active when a notification comes in. On iOS 4.0 or later, if your app was suspended in the background it is woken up and this method is also called. You can use UIApplication’s applicationState property to find out whether your app was suspended or not.
Add Observer to register for events in order to call back when notification recieves :
[[NSNotificationCenter defaultCenter]  addObserver:menuVCDriver selector:@selector(showBookViewController:) name:@"bookingDetails" object:nil];
post the notification as app receives it :
[[NSNotificationCenter defaultCenter] postNotificationName:@"bookingDetails" object:self userInfo:dictBookingDetails];

For more information you can check this tutorial:

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

No comments:

Post a Comment