博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS:APNS推送主要代码
阅读量:7237 次
发布时间:2019-06-29

本文共 3380 字,大约阅读时间需要 11 分钟。

首先,在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。

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5132759.html
,如需转载请自行联系原作者
你可能感兴趣的文章
修改eclipse默认字体
查看>>
消息队列_RabbitMQ-0003.深入RabbitMQ节点/配置/管理及日志实时化?
查看>>
我的友情链接
查看>>
WAMP5安装遇到的问题
查看>>
我的友情链接
查看>>
python logging模块
查看>>
Jquery操作easy-ui表单
查看>>
SVN配置手册第二版
查看>>
Python并发之Gevent
查看>>
个性定制你的 Git 命令行提示符
查看>>
将博客搬至CSDN
查看>>
我的友情链接
查看>>
数据库(升级)迁移
查看>>
nfs文件不共享,Stale file handle
查看>>
电脑死机、开机死机故障原因汇总(硬件)
查看>>
Bootstrap中的段落和强调内容
查看>>
我们如何理解虚拟化、云计算(二)
查看>>
springboot 集成 websocket
查看>>
linux下oralcle11g使用edit命令默认调用vi编辑器
查看>>
Nginx网站服务
查看>>