• 【iOS学习笔记】IOS开发中设置applicationIconBadgeNumber和消息推送


    因为在IOS8中要想设置applicationIconBadgeNumber,需要用户的授权,在IOS8中,需要加上下面的代码:

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
     

    但是这两句如果是在IOS7的系统中运行的话就会报错,所以需要先判断IOS的版本,完整代码如下: 

    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
         
    if (version >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }

    如果是注册消息推送的话,需要这样写: 

    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
     
    if (version >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    }
     

    向操作系统申请后台运行的资格,能维持多久,是不确定的

     UIBackgroundTaskIdentifier task = [application beginBackgroundTaskWithExpirationHandler:^{

           当申请的后台运行时间已经结束(过期),就会调用这个block

            

            赶紧结束任务

            [application endBackgroundTask:task];

            

        }];

  • 相关阅读:
    NodeJS NPM 镜像使用方法
    用for; while...do; do...while; 写出九九乘法表
    create-react-app创建的项目中registerServiceWorker.js文件的作用
    前端应该从哪些方面优化网站?
    JS基础整理面试题
    netcore实践:跨平台动态加载native组件
    iOS开发--Swift RAC响应式编程初探
    算法导论学习笔记 (页码:9 ~ 16)
    iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条
    iOS开发--面试
  • 原文地址:https://www.cnblogs.com/pjl0426/p/4931425.html
Copyright © 2020-2023  润新知