標籤:ios 本地推送 uilocalnotification
建立通知
UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { NSDate *now = [NSDate new]; notification.fireDate = [now dateByAddingTimeInterval:10]; //10秒後通知 notification.repeatInterval=0; //重複次數,kCFCalendarUnitWeekday一周一次 notification.timeZone = [NSTimeZone defaultTimeZone]; //設定時區 notification.applicationIconBadgeNumber = 1; //應用的角標 notification.soundName = UILocalNotificationDefaultSoundName; //聲音,可以換成alarm.soundName = @"sound.wav" //去掉下面2行就不會彈出提示框 notification.alertBody = @"通知內容"; //提示資訊 彈出提示框 notification.alertAction = @"開啟"; //提示框按鈕 //notification.hasAction = NO; //是否顯示額外的按鈕,為no時alertAction消失 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"]; //設定userinfo 方便在之後需要撤銷的時候使用 也可以傳遞其他值,當通知觸發時可以擷取 notification.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
推送的內容
//推送的內容- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{ //這裡,你就可以通過notification的useinfo,幹一些你想做的事情了 if ([[notification.userInfo objectForKey:@"key"] isEqualToString:@"value"]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系統提示" message:@"你的系統存在嚴重威脅" delegate:nil cancelButtonTitle:@"關閉" otherButtonTitles:nil,nil]; [alert show]; } application.applicationIconBadgeNumber = 0; //移除角標}
- (void)applicationDidBecomeActive:(UIApplication *)application{ //不通過推送 通過應用表徵圖開啟應用 移除角標 application.applicationIconBadgeNumber = 0;}
取消通知
//擷取當前所有的本地通知 NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications]; if (!notificaitons || notificaitons.count <= 0) { return; } //取消一個特定的通知 for (UILocalNotification *notify in notificaitons) { if ([[notify.userInfo objectForKey:@"key"] isEqualToString:@"value"]) { [[UIApplication sharedApplication] cancelLocalNotification:notify]; break; } } // //取消所有的本地通知// [[UIApplication sharedApplication] cancelAllLocalNotifications];
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
iOS 本地推送 UILocalNotification