本地推送UILocalNotification常用于定期提醒用戶使用該APP械馆,如AirBrush的定期提醒用戶拍照,運(yùn)動(dòng)鍛煉工具的每天鍛煉提醒似袁。
不同于遠(yuǎn)程推送RemoteNotification株依,本地推送一般較固定胚吁,通常事先設(shè)置好推送周期。而推送內(nèi)容往往也是固定的书聚,可存放于plist文件中唧领。本地推送并不依賴于網(wǎng)絡(luò)連接,可簡(jiǎn)單將其視為一個(gè)定時(shí)裝置即可雌续。
申請(qǐng)通知權(quán)限
若要通知生效斩个,則先要為該APP申請(qǐng)通知權(quán)限。一般可在AppDelegate的application:didFinishLaunchingWithOptions方法中添加如下代碼:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
UILocalNotification對(duì)象
添加本地推送的步驟如下:
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對(duì)象的參數(shù)比較多驯杜,但作用都很明確萨驶。
fireDate設(shè)置觸發(fā)通知接收方法的時(shí)間,
alertTitle和alertBody可設(shè)置相應(yīng)的通知內(nèi)容艇肴。
注意userInfo可用于在該UILocalNotification對(duì)象中傳遞一些參數(shù)腔呜,userInfo必須是NSDictionary類型叁温。
application:didReceiveLocalNotification:方法
該方法用于接收到本地通知時(shí)的回調(diào)方法:
// APP運(yùn)行中收到notification時(shí)調(diào)用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// 可根據(jù)notification對(duì)象的userInfo等屬性進(jìn)行相應(yīng)判斷和處理
}
多個(gè)本地通知
一般的工具型APP會(huì)包含多個(gè)本地通知,分別設(shè)置不同的fireDate核畴。如三天膝但,7天,一個(gè)月分別推送一次谤草,以喚醒用戶跟束。若一個(gè)月之內(nèi)打開APP,則所有本地通知重置丑孩。
這里以設(shè)置1Min和3Min的本地通知為例:
可在AppDelegate的applicationDidBecomeActive:方法中設(shè)置如下冀宴,則每次APP啟動(dòng)即執(zhí)行:
-(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上的消息提示個(gè)數(shù)
[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:方法接收本地通知的時(shí)間參數(shù),如下:
-(void)setLocalNotification:(NSTimeInterval)timeInterval {
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
// 設(shè)置提醒時(shí)間為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];
}
}
這里温学,簡(jiǎn)單介紹了UILocalNotification的常見使用方式略贮,往往用于固定周期內(nèi)喚醒用戶。