当前位置: 首页 > news >正文

iOS:APNS推送主要代码

首先,在AppDelegate.m 中:

1,注册通知

//[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    // Override point for customization after application launch.  
    ViewController *mainCtrl=[[ViewController alloc] init];  
    self.window.rootViewController=mainCtrl;  
      
    //注册通知  
    if ([UIDevice currentDevice].systemVersion.doubleValue<8.0) {  
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];  
    }  
    else {  
        [[UIApplication sharedApplication] registerForRemoteNotifications];  
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];  
    }  
      
    //判断是否由远程消息通知触发应用程序启动  
    if (launchOptions) {  
        //获取应用程序消息通知标记数(即小红圈中的数字)  
        NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;  
        if (badge>0) {  
            //如果应用程序消息通知标记数(即小红圈中的数字)大于0,清除标记。  
            badge--;  
            //清除标记。清除小红圈中数字,小红圈中数字为0,小红圈才会消除。  
            [UIApplication sharedApplication].applicationIconBadgeNumber = badge;  
            NSDictionary *pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];  
              
            //获取推送详情  
            NSString *pushString = [NSString stringWithFormat:@"%@",[pushInfo  objectForKey:@"aps"]];  
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"finish Loaunch" message:pushString delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];  
            [alert show];  
        }  
    }  
      
    return YES;  

2,注册通知后,获取device token

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  
    NSString *token = [NSString stringWithFormat:@"%@", deviceToken];  
    NSLog(@"My token is:%@", token);  
    //这里应将device token发送到服务器端  
}  
  
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {  
    NSString *error_str = [NSString stringWithFormat: @"%@", error];  
    NSLog(@"Failed to get token, error:%@", error_str);  
}  

3,接收推送通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
{  
    [UIApplication sharedApplication].applicationIconBadgeNumber=0;  
    for (id key in userInfo) {  
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);  
    }  
    /* eg. 
    key: aps, value: { 
        alert = "\U8fd9\U662f\U4e00\U6761\U6d4b\U8bd5\U4fe1\U606f"; 
        badge = 1; 
        sound = default; 
    } 
     */  
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"remote notification" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];  
    [alert show];  
}  

注意:app 前台运行时,会调用 remote notification;app后台运行时,点击提醒框,会调用remote notification,点击app 图标,不调用remote notification,没反应;app 没有运行时,点击提醒框,finishLaunching   中,launchOptions 传参,点击app 图标,launchOptions 不传参,不调用remote notification。

转载于:https://www.cnblogs.com/XYQ-208910/p/5132759.html

相关文章:

  • 上周热点回顾(1.11-1.17)
  • iOS之旅--隐藏(去除)导航栏底部横线
  • JVM内存机制
  • 浅谈MVVM架构
  • Python执行需要经过哪些过程
  • OSI
  • 例题 3-6 环状序列
  • JQuery中使用Ajax实现诸如登录名检测等异步请求Demo
  • java String、Data、Calendar时间转化
  • js 打印
  • Java NIO读书笔记
  • angular
  • Oracle使用总结
  • 通过keepalived实现 MySQL VIP 自动切换
  • Swift - 37 - 值类型和引用类型的简单理解
  • Angular 响应式表单 基础例子
  • create-react-app做的留言板
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • Laravel5.4 Queues队列学习
  • Linux各目录及每个目录的详细介绍
  • Meteor的表单提交:Form
  • React中的“虫洞”——Context
  • Zepto.js源码学习之二
  • 编写符合Python风格的对象
  • 第十八天-企业应用架构模式-基本模式
  • 对超线程几个不同角度的解释
  • 诡异!React stopPropagation失灵
  • 聊一聊前端的监控
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • ​queue --- 一个同步的队列类​
  • ​Spring Boot 分片上传文件
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (4)logging(日志模块)
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (算法设计与分析)第一章算法概述-习题
  • (一)插入排序
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)编辑寄语:因为爱心,所以美丽
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .libPaths()设置包加载目录
  • .net core控制台应用程序初识
  • .NET NPOI导出Excel详解
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .net 使用ajax控件后如何调用前端脚本
  • .NET/C# 使窗口永不获得焦点
  • .Net各种迷惑命名解释
  • .net连接MySQL的方法
  • :“Failed to access IIS metabase”解决方法
  • :如何用SQL脚本保存存储过程返回的结果集
  • ??在JSP中,java和JavaScript如何交互?
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)
  • [BUUCTF]-Reverse:reverse3解析