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。

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5132759.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
移动开发 安全 数据安全/隐私保护
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
|
3月前
|
移动开发 前端开发 安全
最强大的 iOS 应用源码保护工具:Ipa Guard,保护你的商业机密代码
最强大的 iOS 应用源码保护工具:Ipa Guard,保护你的商业机密代码
|
3月前
|
移动开发 前端开发 数据安全/隐私保护
【工具】iOS代码混淆工具-iOS源码混淆
【工具】iOS代码混淆工具-iOS源码混淆
42 1
|
6月前
|
移动开发 前端开发 数据安全/隐私保护
iOS代码混淆-从入门到放弃
iOS代码混淆-从入门到放弃
88 0
|
2月前
|
移动开发 安全 数据安全/隐私保护
iOS 代码混淆和加固技术详解
iOS 代码混淆和加固技术详解
|
2月前
|
移动开发 前端开发 数据安全/隐私保护
iOS 代码混淆 - 从入门到放弃
iOS 代码混淆 - 从入门到放弃
|
2月前
|
安全 算法 数据安全/隐私保护
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
|
3月前
|
安全 Java Android开发
iOS代码安全加固利器:深入探讨字符串和代码混淆器的作用
iOS代码安全加固利器:深入探讨字符串和代码混淆器的作用
35 0
|
3月前
|
移动开发 安全 前端开发
iOS代码混淆工具
iOS代码混淆工具
60 1
|
3月前
|
安全 开发工具 Swift
ios-class-guard - iOS代码混淆与加固实践
ios-class-guard - iOS代码混淆与加固实践
48 0