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];