通過iOS的UILocalNotification Class可以實現本地app的定時推送功能,即使當前app是後台關閉狀態。
可以實現諸如,設置app badgenum,彈出一個alert,播放聲音等等,實現很簡單
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
NSDate *now=[NSDate new];
notification.fireDate=[now dateByAddingTimeInterval:15];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"定時推送通知!";
notification.soundName = @"default";
[notification setApplicationIconBadgeNumber:22];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
寫了一個demo,大家直接看demo就一目了然了,很方便,在適當場合使用還是蠻實用的~
源代碼鏈接:https://github.com/andypan1314/LocalNotificationTest
iOS 設置每天下午4點推送本地通知
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {//判斷系統是否支持本地通知
notification.fireDate = [NSDate dateWithTimeIntervalSince1970:16*60*60*24];//本次開啟立即執行的周期
notification.repeatInterval=kCFCalendarUnitWeekday;//循環通知的周期
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"哇哇哇";//彈出的提示信息
notification.applicationIconBadgeNumber=0; //應用程序的右上角小數字
notification.soundName= UILocalNotificationDefaultSoundName;//本地化通知的聲音
//notification.alertAction = NSLocalizedString(@"美女呀", nil); //彈出的提示框按鈕
notification.hasAction = NO;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。