Local notifications are best way to retain user in your game or app.It gives alert to users via app that might not be running.
Local notifications can be scheduled at the time when user's app is running in foreground or background.You just need to schedule notification to be triggered at a specific time, It's the system who manages to deliver it at appropriate time. |
1) It's not necessary for the app to be running when the system is about to deliver notification
2)The user gets message with an alert panel or banner, with a sound, or by badging your app’s icon.
3)You can handle notification internally inside the app, when app is running in foreground and notification arrives.
Step1:
You need to add UserNotifications.framework into your project and import it AppDelegate.h class.
Step2:
Also, include UNUserNotificationCenterDelegate in AppDelegate class.
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
Step3:
Define a constant in AppDelegate.m to check version of iOS .
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
Step4:
Now,you need to Register for PushNotification and define a method as mentioned below.
– (void)registerForRemoteNotifications {
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@”10.0″)){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if(!error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
} else {
// Code for old versions
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
Step5:
You need to add this code in your applicationDidFinishLaunchingmethod method.–
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForRemoteNotifications];
return YES;
}
How to Retrieve Device Token and save it for using later?
– (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *deviceTokenString = [deviceToken description];
deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”<>”]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@” ” withString:@””];
NSLog(@”Push Notification deviceTokenString is %@”,deviceTokenString);
[[NSUserDefaults standardUserDefaults]setObject:deviceTokenString forKey:@”deviceTokenString”];
[[NSUserDefaults standardUserDefaults]synchronize];
}
How to Handle UserNotifications delegate methods ?
Note:
From iOS10 or later, We are able to alert notification when app is running in foreground.
Here is the method called when a notification is delivered to a foreground state of the app
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@”User Info : %@”,notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
[self handleRemoteNotificationMethod:[UIApplication sharedApplication] userInfo:notification.request.content.userInfo];
}
Here is the method called to detect what action was taken by the user for a given notification within your app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@”User Info : %@”,response.notification.request.content.userInfo);
completionHandler();
[self handleRemoteNotificationMethod:[UIApplication sharedApplication] userInfo:response.notification.request.content.userInfo];
}
Methods to handle Remote Notification
-(void) handleRemoteNotificationMethod:(UIApplication *) application userInfo:(NSDictionary *) remoteNotif {
NSLog(@”handleRemoteNotification”);
NSLog(@”Handle Remote Notification Dictionary: %@”, remoteNotif);
// You need to handle Click of the Push Notification From Here…
// You can write a code to redirect user to specific screen of the app here….
}
Final Step:You need to add Push Notifications Entitlements
Finally,you need to enable Remote push notifications in your project
Go to project->target->capabilities and enable notification switch as follows