iOS --- 本地推送通知 UILocalNotification

本地推送UILocalNotification常用于定期提醒用户使用该APP,如AirBrush的定期提醒用户拍照,运动锻炼工具的每天锻炼提醒。
不同于远程推送RemoteNotification,本地推送一般较固定,通常事先设置好推送周期。而推送内容往往也是固定的,可存放于plist文件中。本地推送并不依赖于网络连接,可简单将其视为一个定时装置即可。

申请通知权限

若要通知生效,则先要为该APP申请通知权限。一般可在AppDelegate的application:didFinishLaunchingWithOptions方法中添加如下代码:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { 
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}

UILocalNotification对象

添加本地推送的步骤如下:

UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) { 
    NSLog(@">> 10s' local notification"); 
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    notification.alertTitle = @"AlertTitle 1"; 
    notification.alertBody = @"AlertBody 1"; 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"notification_1", @"id", nil]; 
    notification.userInfo = infoDict; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

UILocalNotification对象的参数比较多,但作用都很明确。
fireDate设置触发通知接收方法的时间,
alertTitle和alertBody可设置相应的通知内容。
注意userInfo可用于在该UILocalNotification对象中传递一些参数,userInfo必须是NSDictionary类型。

application:didReceiveLocalNotification:方法

该方法用于接收到本地通知时的回调方法:

// APP运行中收到notification时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    // 可根据notification对象的userInfo等属性进行相应判断和处理
}

多个本地通知

一般的工具型APP会包含多个本地通知,分别设置不同的fireDate。如三天,7天,一个月分别推送一次,以唤醒用户。若一个月之内打开APP,则所有本地通知重置。
这里以设置1Min和3Min的本地通知为例:
可在AppDelegate的applicationDidBecomeActive:方法中设置如下,则每次APP启动即执行:

-(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.  
    // appIcon上的消息提示个数 
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;  
    // 取消所有的本地通知 
    [[UIApplication sharedApplication] cancelAllLocalNotifications];  
    // 添加1Mins和3Mins的localNotification

#define kLocalNotificationTimeInterval_1Mins (60*1)
#define kLocalNotificationTimeInterval_3Mins (60*3) 

    [self setLocalNotification:kLocalNotificationTimeInterval_1Mins]; 
    [self setLocalNotification:kLocalNotificationTimeInterval_3Mins];
}

setLocalNotification:方法接收本地通知的时间参数,如下:

-(void)setLocalNotification:(NSTimeInterval)timeInterval { 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    if (notification) { 
        // 设置提醒时间为20:00 
        //NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
        //NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]]; 
        //dateComponents.hour = 20; 
        //dateComponents.minute = 0; 
        //NSDate *fireDate = [calendar dateFromComponents:dateComponents];  
        NSDate *fireDate = [NSDate date];  
        notification.fireDate = [fireDate dateByAddingTimeInterval:timeInterval]; 
        notification.timeZone = [NSTimeZone defaultTimeZone]; 
        notification.alertBody = [NSString stringWithFormat:@"Local Notification %f", timeInterval]; 
        notification.applicationIconBadgeNumber = 1; 

#define LocalNotificationPeriod_1Mins @"LocalNotificationPeriod_1Mins"
#define LocalNotificationPeriod_3Mins @"LocalNotificationPeriod_3Mins"  
    
        NSString *period; 
        if (timeInterval == kLocalNotificationTimeInterval_1Mins) { 
            period = LocalNotificationPeriod_1Mins; 
        } else { 
            period = LocalNotificationPeriod_3Mins; 
        }  
        notification.userInfo = @{ @"id": period, }; 
        [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    }
}

这里,简单介绍了UILocalNotification的常见使用方式,往往用于固定周期内唤醒用户。

DemoDemo地址: DemoLocalNotification

推荐阅读更多精彩内容

  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 3,451评论 12 16
  • 推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的,不可...
    iOS开发攻城狮阅读 1,868评论 1 12
  • 推送通知注意:这里说的推送通知跟NSNotification有所区别NSNotification是抽象的,不可见的...
    醉叶惜秋阅读 585评论 0 3
  • 推送通知跟NSNotification不同 1.NSNotification是抽象的,不可见的 2.推送通知是可见...
    NBeanN阅读 116评论 0 0
  • —— 欧阳修 环滁皆山也。 其西南诸峰, 林壑尤美, 望之蔚然而深秀者, 琅琊也。 山行六七里, 渐闻水声潺潺而泻...
    岚风的叶子阅读 38评论 0 0