UILocalNotification a local notifications

In ios development, often used to function notification. The notice is divided into local notification and remote notification. There are many remote notification, you can use third-party implementations. This article describes how to use local notifications, and remote notification will tell you later. To achieve local notification function is very simple, just a class is enough. This class is UILocalNotification. It will be able to easily implement functions for local notification through. The following directly on the code

/**
 *  设置本地通知
 */
- (void)settingNotication{
    // 判断是否是 ios8以上的系统。如果是 ios8 只需要主动向用户注册通知设置(比如是否运行弹框,设置图标右上角数字等)
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
    }
    // 创建本地推送对象
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置通知触发时间
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 设置时区(为空则使用默认时区,该属性值决定了fireDate的时间)
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置推送重复间隔时间(这里设置了一分钟推送一次通知)
    notification.repeatInterval = NSCalendarUnitMinute;
    // 设置通知声音(可以指定一个声音文件名,包括扩展名。声音文件时长不能超过30秒,超过30秒系统会使用默认的声音)
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 设置通知内容(该属性值为 nil,则通知触发了也不会有任何提示)
    notification.alertBody = @"是时候吃早餐了";
    // 设置应用图片右上角显示的文字
    notification.applicationIconBadgeNumber = 10;
    // 设置用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图片
    // 该属性在 ios8下没有效果
    notification.alertLaunchImage = @"images";
    // 在锁屏情况下,该属性替换“slide to unlock” 中“unlock”
    notification.alertAction = @"去吃早餐";
    // 传递数据
    notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"测试通知001",@"notificationId", nil];
    
    // 将通知登记到系统Notification处理队列中(这一步非常重要,没有这一步,创建出来的通知可以说废的)
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

Run renderings

   

A figure: the case where no lock screen

   

Figure 2: lockscreen

How to deal with UILocalNotification?

After the reminder box action button is clicked, the application begins to run is possible in - (BOOL) application: didFinishLaunchingWithOptions: Application delegate this processing method. You can load unprocessed recent notice in the following manner.UILocalNotification * localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; if the application is running, can be covered by a method in Application Delegate

- (void) application: didReceiveLocalNotification: to deal with Notification. Method as the second parameter is UILocalNotification objects,

Simply carrying userInfo processed to a processing operation in response.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification {
    // 处理接收到的本地通知
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒p" message:notification.alertBody
     delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    //在这里你就可以通过notification的useinfo做自己想做的事情
    // ..........................
    // 取消某个特定的本地通知
    for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        NSString *notiID = noti.userInfo[@"notificationId"];
        NSString *receiveNotiID = notification.userInfo[@"notificationId"];
        if ([notiID isEqualToString:receiveNotiID]) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            return;
        }
    }
}
to sum up

Local notifications mechanism is very effective in application development, it can be a good help developers manage events need to occur some designated time, such as alarm clock application class. And because the system of unified management of local notifications, so that the same task can be very simple and have been processed, without wasting resources to make the application waits for a trigger event.


发布了10 篇原创文章 · 获赞 1 · 访问量 5890

Guess you like

Origin blog.csdn.net/pkxwyf/article/details/41820235