- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
//先向用户请求授权,如果用户同意通知就注册一个通知,否则就请求授权
if([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone){
//注册一个通知
[self addLocalNotification];
}else{
//请求授权,并设置通知的类型.有提示框,App图标右上角提示的数字,有提示的声音
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
//用户授权后调用
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//如果用户许可通知,就添加一个通知
if(notificationSettings.types != UIUserNotificationTypeNone){
[self addLocalNotification];
}
}
//打开应用
- (void)applicationWillEnterForeground:(UIApplication *)application
{
//将icon的右上角的数字设置为0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
#pragma mark - 添加一个本地通知
- (void)addLocalNotification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];//10秒后提醒
notification.repeatInterval = 2;//重复次数
notification.alertBody = @"好久不玩了,来玩吧";//提示的信息
notification.applicationIconBadgeNumber = 1;//右上角的数字
notification.alertAction = @"打开App";//滑动解锁时的副标题提示语句
notification.alertLaunchImage = @"1.png";//滑动解锁进入应用时显示的图片
notification.soundName = @"msg.caf";//需要真机才能听到声音
notification.userInfo = @{@"id":@1,@"user":@"LuCa"};//通知上的一些附加信息
//调用通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
//如果不需要本地的通知时,就移除该通知
- (void)removeNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
原文地址:http://blog.csdn.net/lu_ca/article/details/46550959