码迷,mamicode.com
首页 > 移动开发 > 详细

iOS本地推送

时间:2015-06-18 22:20:15      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ios   推送   本地推送   

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

}



iOS本地推送

标签:ios   推送   本地推送   

原文地址:http://blog.csdn.net/lu_ca/article/details/46550959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!