美文网首页
UILocalNotification 实现本地提醒

UILocalNotification 实现本地提醒

作者: Maggie的小蜗居 | 来源:发表于2016-11-06 21:17 被阅读108次

    UILocalNotification 实现本地提醒

    属性有:
    *fireDate 通知开始的确切时间
    *timeZone 通知开始时间是否根据时区改变,设为nil,则不会更改
    *repeatInterval UILocalNotification被重复激发之间的时间差
    *repeatCalendar :NSWeekCalendarUnit 每周激发的频率
    *alertBody 弹出的内容
    *soundName 推送声音UILocalNotificationDefaultSoundName
    *applicationIconBadgeNumber 显示在icon上的红色圈中的数子

    1.获取本地提醒的权限

            if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
            
            // iOS8.0
            UIUserNotificationType types =
            UIUserNotificationTypeBadge|
            UIUserNotificationTypeSound|
            UIUserNotificationTypeAlert;
            
            UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            
            [application registerUserNotificationSettings:mySettings];
        }
    

    2. 创建一个本地通知:

        UILocalNotification *notification = [[UILocalNotification alloc] init];
       notification.fireDate = fireDate;
       notification.timeZone = [NSTimeZone defaultTimeZone];
       notification.repeatCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
       notification.repeatInterval = 2;
       notification.alertBody = @"起床啦";
       notification.soundName = @"default";
       //添加推送到uiapplication
       [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    

    3.程序运行时接收到本地推送消息

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
    
    {
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"local noti"
    
        message:notification.alertBody
    
        delegate:nil
    
        cancelButtonTitle:@"cancel"
    
        otherButtonTitles:nil];
    
        [alert show];
    
        //可以通过notification的useinfo,do something
    
        application.applicationIconBadgeNumber -= 1;
    
    }
    
    
    

    4.取消通知

    • 取消所有通知
      [[UIApplication sharedApplication] cancelAllLocalNotification];

    • 取消指定的通知

        UIApplication *app = [UIApplication sharedApplication];
    
        NSArray *localArray = [app scheduledLocalNotifications];
    
        for (UILocalNotification *noti in localArr) {
            NSDictionary *userInfo = noti.userInfo;
            NSNumber *obj = [userInfo objectForKey:@"key"];
            int tag=[obj intValue];
            if tag == 12 {
                [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
                break;
    
            }
        }
    
    

    相关文章

      网友评论

          本文标题:UILocalNotification 实现本地提醒

          本文链接:https://www.haomeiwen.com/subject/dxynuttx.html