使用過的代碼火欧,直接貼上
?UILocalNotification *notification = [[UILocalNotification alloc] init];
? ?if (notification!=nil) {
? ? ?NSDate *now = [NSDate new];
? ? ?//從現(xiàn)在開始,10秒以后通知
? ? ?notification.fireDate=[now addTimeInterval:10];
? ? ?//使用本地時(shí)區(qū)
? ? ?notification.timeZone=[NSTimeZone defaultTimeZone];
? ? ?notification.alertBody=@"頂部提示內(nèi)容妨托,通知時(shí)間到啦";
? ? ?//通知提示音 使用默認(rèn)的
? ? ?notification.soundName= UILocalNotificationDefaultSoundName;
? ? ?notification.alertAction=NSLocalizedString(@"你鎖屏啦份氧,通知時(shí)間到啦", nil);
? ? ?//這個(gè)通知到時(shí)間時(shí)存崖,你的應(yīng)用程序右上角顯示的數(shù)字静暂。
? ? ?notification.applicationIconBadgeNumber = 1;
? ? ?NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
? ? ?notification.userInfo = dic;
? ? ?//啟動(dòng)這個(gè)通知
? ? ?[[UIApplication sharedApplication]scheduleLocalNotification:notification];
?}
需要注意的是在iOS8之后需要注冊(cè)消息推送服務(wù)才可以聊倔,具體實(shí)現(xiàn)就在AppDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中直接調(diào)用下面方法即可
?if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
? ? ? ?[application registerUserNotificationSettings:[UIUserNotificationSettings
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? categories:nil]];
?}