Friday, 7 November 2014

Local Notification in iOS8

Hi All,

This is my first blog post for iOS8. In this blog post I am going to create a simple Local Notification. In earlier version of iOS( upto iOS7 ), we don't have to ask permission from user whether creating LocalNotification or not. But from iOS8 onwards, we have to ask user for permission same as we are asking permission for Push Notification, Contacts, Gallery, etc...

Here we are going to create a demo application of how we have to ask for user permission for Local Notification and create a Local Notification.

1) First create a project in Xcode6 (iOS8 will only be available in Xcode6, if you have older Xcode please update it.), a Single View Application. Below is its screenshot.

2) Enter project name (whatever your project name), enter Organization Name, enter Organization Identifier, select Language (Swift OR Objective-C) (In this tutorial we are going to use Objective-C.), Click Next.

3) Save it wherever you want.

4) Now, we have to write code for asking user permission for Local Notification. Here I am writibf in AppDelegate's - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

Here is its Code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    // None of the code should even be compiled unless the Base SDK is iOS 8.0 or later
    // The following line must only run under iOS 8. This runtime check prevents
    // it from running if it doesn't exist (such as running under iOS 7 or earlier).
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    return YES;

6) Now run the project, it will ask for user permission.

7) Now we are going to create a Local Notification, go to ViewController.m's viewDidLoad method and write this code

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    localNotification.alertBody = @"Local Notification in iOS8";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

This simple code will create a LocalNotification that will fire after 1 minute of application launch. As soon as you launch the application press command + shift + H (to go to home screen), else you will not be able to view LocalNotification.

Note :- If you don't ask for user permission and try to create LocalNotification, compiler will generate a warning similar to this
Local Notification in iOS8[667:14153] Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7bf758e0>{fire date = Saturday, November 8, 2014 at 9:13:02 AM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 8, 2014 at 9:13:02 AM India Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts
and it will not create LocalNotification.

This demo project is uploaded on Github, you can download it from here.

If anyone have any question regarding this, they can write comments below.