標籤:
使用過的代碼,直接貼上
1 UILocalNotification *notification = [[UILocalNotification alloc] init]; 2 if (notification!=nil) { 3 NSDate *now = [NSDate new]; 4 //從現在開始,10秒以後通知 5 notification.fireDate=[now addTimeInterval:10]; 6 //使用本地時區 7 notification.timeZone=[NSTimeZone defaultTimeZone]; 8 notification.alertBody=@"頂部提示內容,通知時間到啦"; 9 //通知提示音 使用預設的10 notification.soundName= UILocalNotificationDefaultSoundName;11 notification.alertAction=NSLocalizedString(@"你鎖屏啦,通知時間到啦", nil);12 //這個通知到時間時,你的應用程式右上方顯示的數字。13 notification.applicationIconBadgeNumber = 1;14 NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];15 notification.userInfo = dic;16 //啟動這個通知17 [[UIApplication sharedApplication]scheduleLocalNotification:notification];18 }
需要注意的是在iOS8之後需要註冊訊息推送服務才可以,具體實現就在AppDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中直接調用下面方法即可
1 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {2 [application registerUserNotificationSettings:[UIUserNotificationSettings 3 settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound 4 categories:nil]];5 }
使用UILocalNotification給App添加本地訊息通知