Powered by
Powered by
HuntMyIdeas-Inspire You To Make It Happen
Follow
   Me
  • Blogs
  • About Me
  • Home

iOS - How to implement Local Notifications using Objective C

11/12/2017

0 Comments

 
Picture
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.
Notes:
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.
1.How to implement Local Notification from iOS 10 and later ?

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

Picture
Read:
iOS Interview Questions And Answers For 1-2 years Experienced Candidates.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Enter your email address:

    Delivered by FeedBurner

    RSS Feed

    Archives

    December 2019
    August 2018
    January 2018
    December 2017
    November 2017
    October 2017
    August 2017
    April 2017
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    July 2014
    June 2014


    ​​Categories

    All
    Affiliate Marketing
    Alexa Rank
    Apple's Latest Updates
    Blog Tips
    Cocos2d X
    Cocos2d-x
    Google AdSense Tips
    Grab-some-ultimate-ideas
    How-to-create-a-twinkling-star-background-effect-in-ios
    In-App Purchase Tutorial In Cocos2d-x (C++) Using SDKBOX
    Ios Interview Questions And Answers
    IOS Multiple Choice Questions And Answers
    Objective C
    Online Business
    Python
    Secrets To Effective IPhone App Marketing
    SEO Tips
    Swift Interview Questions
    Swift Tutorials
    Top 10 Articles Objective C
    Top 10 Articles-Objective-C
    Top 10 Popular Movies Ever
    Top 10 Popular Things To Know
    Top 10 Ultimate Issues Xcode
    Top 400 IOS Interview Questions
    Top IOS Sample Projects To Learn From
    Top Secrets To App Marketing
    UICollectionView
    Unity 3D
    Xcode

WELCOME TO HUNTMYIDEAS

Picture
I am Jitendra Gochhayat,an iOS Developer,Designer,blogger,
​from Odisha,India.
I write about ​​​blog tips and,
Make money online Tips,
​Objective C tutorials
SUSCRIBE TO NEWSLETTER

    Subscribe to this blog and receive notifications of new posts by email

Suscribe
Picture
Picture
Picture
Picture
Picture
HuntMyIdeas-2016 Powered By Weebly
Picture
Powered by Create your own unique website with customizable templates.
Powered by
Powered by