How to implement LocalNotification using objective C?

后端 未结 2 1039
逝去的感伤
逝去的感伤 2021-01-02 10:14

I am trying to implement local notification into my application. I don\'t know how to do properly, below code i am using for new data arrival proce

2条回答
  •  时光取名叫无心
    2021-01-02 10:40

    1) When the app is closed, schedule a local notification that will fire in 24 hours

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
        notification.alertBody = @"24 hours passed since last visit :(";
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    

    2) if the app is opened (before the local notification fires), cancel the local notification

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }
    

    //For local Notification

    first thing we need to do is register the notifications.

     // New for iOS 8 - Register the notifications
            UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
            UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    

    Now let’s create the notification itself

        UILocalNotification *notification = [[UILocalNotification alloc] init];
        if (notification)
        {
                notification.fireDate = _datePicker.date;
    
                NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
                notification.fireDate = fireTime;
                notification.alertBody = @"Alert!";
    
                notification.timeZone = [NSTimeZone defaultTimeZone];
                notification.applicationIconBadgeNumber = 1;
                notification.soundName = UILocalNotificationDefaultSoundName;
                switch (_frequencySegmentedControl.selectedSegmentIndex) {
                    case 0:
                        notification.repeatInterval = NSCalendarUnitDay;
                        break;
                    case 1:
                        notification.repeatInterval = NSCalendarUnitWeekOfYear;
                        break;
                    case 2:
    
    
               notification.repeatInterval = NSCalendarUnitYear;
                    break;
                default:
                    notification.repeatInterval = 0;
                    break;
            }
            notification.alertBody = _customMessage.text;
    

    Once we have the notification created we need to schedule it with the app.

    // this will schedule the notification to fire at the fire date
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    // this will fire the notification right away, it will still also fire at the date we set
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    

    If we leave things the way they are now a notification will only appear on screen if the app is in the background. In order to display something when the app is in the foreground and a notification fires we need to implement a method in the app delegate.

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification Received" message:notification.alertBody delegate:nil     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
    }
    

    We added a icon badge to our app, and this icon badge will only display when the app is in the background. Generally you want to dismiss the icon once a user has opened the app and seen the notification. We’ll need to handle this in the app delegate as well.

    These two methods will take care of it.

    - (void)applicationWillEnterForeground:(UIApplication *)application 
    {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        NSLog(@"%s", __PRETTY_FUNCTION__);
        application.applicationIconBadgeNumber = 0;
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application 
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        NSLog(@"%s", __PRETTY_FUNCTION__);
        application.applicationIconBadgeNumber = 0;
    }
    

提交回复
热议问题