標籤:
網路推送可能被人最為重視,但是本地推送有時候項目中也會運用到;
閑話少敘,代碼如下:
1、添加根視圖
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
2、本地建立一個button進行觸發
button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(20, 100, WIDTH - 40, 50); button.backgroundColor = [UIColor blueColor]; [button setTitle:@"開始啦" forState:UIControlStateNormal]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //Binder 方法 [button addTarget:self action:@selector(noticClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
3、註冊一個通知
//設定本地通知 傳一個時間進去+ (void)registerLocalNotification:(NSInteger)alertTime{ UILocalNotification *notification = [[UILocalNotification alloc]init]; //設定觸發通知的時間 NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime]; NSLog(@"fireDate=%@",fireDate); notification.fireDate = fireDate; //時區 notification.timeZone = [NSTimeZone defaultTimeZone]; //設定重複的間隔 notification.repeatInterval = kCFCalendarUnitSecond; //通知內容 notification.alertBody = @"該起床了..."; notification.applicationIconBadgeNumber = 1; //通知被觸發時播放的聲音 notification.soundName = UILocalNotificationDefaultSoundName; //通知參數 NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"起床了,開始學習了ios開發了" forKey:@"key"]; notification.userInfo = userDict; //ios8 以後,需要添加這個註冊,才能得到授權 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; // 通知重複提示的單位,可以是天、周、月 notification.repeatInterval = NSCalendarUnitDay; } else { // 通知重複提示的單位,可以是天、周、月 notification.repeatInterval = NSDayCalendarUnit; } //執行通知註冊 [[UIApplication sharedApplication]scheduleLocalNotification:notification]; }
4、調用這個方法
-(void)noticClick:(id)sender{ //調用通知 [ViewController registerLocalNotification:4];//4秒鐘後}
5、取消通知的方法
//取消某個本地推播通知+(void)cancelLocalNotificationWithKey:(NSString *)key{ //擷取所有本地通知數組 NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications; for (UILocalNotification *notification in localNotifications) { NSDictionary *userInfo = notification.userInfo; if (userInfo) { //根據設定通知參數時指定的key來擷取通知參數 NSString *info = userInfo[key]; //如果找到需要取消的通知,則取消 if (info != nil) { [[UIApplication sharedApplication]cancelLocalNotification:notification]; break; } } }}
6、調用這個方法
//本地通知回呼函數,當應用程式在前台時調用-(void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification{ NSLog(@"notif:%@",notification); //這裡真是需要處理互動的地方 //擷取通知所帶的資料 NSString *notMes = [notification.userInfo objectForKey:@"key"]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"本地通知(前台)" message:notMes delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; //更新顯示的角標個數 NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber; badge--; badge = badge >= 0 ? badge : 0; [UIApplication sharedApplication].applicationIconBadgeNumber = badge; //在不許要再推送時,可以取消推送 [ViewController cancelLocalNotificationWithKey:@"key"];}
ios 開發之本地推送