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

iOS本地推送的实现,兼容iOS8

时间:2015-02-26 18:37:47      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:ios   本地推送   uilocalnotification   ios8   

AppDelegate.m中的代码

// AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 处理iOS8本地推送不能收到的问题
    float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue;
    if (sysVersion>=8.0) {
        UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
        UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
    }
    return YES;
}

// 接收本地推送(AppDelegate.m中添加)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    
    // 图标上的数字减1
    application.applicationIconBadgeNumber -= 1;
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 直接打开app时,图标上的数字清零
    application.applicationIconBadgeNumber = 0;
}


在调用的类中添加的代码

// 进行推送的方法
// 设置本地推送参数,并进行推送
- (void)scheduleNotification{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    //设置5秒之后
    NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:5];
    if (notification != nil) {
        // 设置推送时间(5秒后)
        notification.fireDate = pushDate;
        // 设置时区(此为默认时区)
        notification.timeZone = [NSTimeZone defaultTimeZone];
        // 设置重复间隔(默认0,不重复推送)
        notification.repeatInterval = 0;
        // 推送声音(系统默认)
        notification.soundName = UILocalNotificationDefaultSoundName;
        // 推送内容
        notification.alertBody = @"推送主体内容";
        //显示在icon上的数字
        notification.applicationIconBadgeNumber = 1;
        //设置userinfo 方便在之后需要撤销的时候使用
        NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
        notification.userInfo = info;
        //添加推送到UIApplication
        UIApplication *app = [UIApplication sharedApplication];
        [app scheduleLocalNotification:notification];
    }
}


iOS本地推送的实现,兼容iOS8

标签:ios   本地推送   uilocalnotification   ios8   

原文地址:http://blog.csdn.net/u011439689/article/details/43954913

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