zoukankan      html  css  js  c++  java
  • Apple Remote Push Notifications

    1、帮助文档参考:

    https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ProvisioningDevelopment.html#//apple_ref/doc/uid/TP40008194-CH104-SW1

    https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1

    2、服务器证书文件生成文档:

    http://fecbob.pixnet.net/blog/post/39286091-蘋果推送apns

    3、APNS全流程:

    http://www.cnblogs.com/gpwzw/archive/2012/03/31/apple_push_notification_services_tutorial_part_1-2.html

    4、Google描述APNS:

    https://code.google.com/p/apns-php/wiki/CertificateCreation

    5、APNs之我见:

    整个流程中的角色: apple push notification server(aServer), the third push notification server(mServer), APP, iphone(device)

    目的:aServer将mServer发送过来的通知推送到device上

    条件:1. mServer证书,因为aServer需要区分是谁发送通知过来

       2. APP证书,因为aServer同样需要知道通知是对应于哪个APP的

       3. device token, aServer需要知道将通知发送到哪个地址

    6、实际操作流程

    1)生成.certSigningRequest文件。方法:apple developer website->APP IDs->选择你需要创建push通知的bundle id编辑Push Notifications那项。有具体文档说明如何生成.certSigningRequest文件

    2) 得到aps_development.cer(或者distribution也行)。上传.certSigningRequest文件文件后,apple会自动生成aps_development.cer文件,直接下载然后双击便装进了keychain中

    3)生成apns-dev.p12(当然可以是其他名字)文件。在keychain中选择aps_development.cer,右键导出为.p12格式便可

    4)生成apns-dev.pem文件。此文件可以直接发给server端用便可。使用此命令(参照上面google的文档):

    openssl pkcs12 -in apns-dev.p12 -out apns-dev.pem -nodes -clcerts

    5)xcode工程设置:注意provisioning profile 的bundleID一定要和aps_development.cer证书的bundleID是一样的

    6) 在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中register push notification

        if (!TARGET_IPHONE_SIMULATOR && ![[NSUserDefaults standardUserDefaults] objectForKey:DeviceTokenKey]) {

            NSLog(@"Registering for push notifications...");

            

             if([[[UIDevice currentDevice] systemVersion] floatValue] > 7.1)

                [[UIApplication sharedApplication] registerForRemoteNotifications];

             else

                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

        }

    7) push notification delegate 

    #pragma mark -

    #pragma mark  Push Notification

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

    {

        NSLog(@"deviceToken: %@", deviceToken);

    if (deviceToken != nil)

        {

            NSString *deviceTokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""]

                                         stringByReplacingOccurrencesOfString:@">" withString:@""]

                                        stringByReplacingOccurrencesOfString:@" " withString:@""];

            

            if (deviceTokenString) {

                NSLog(@"deviceTokenString = %@", deviceTokenString);

                [[NSUserDefaults standardUserDefaults] setObject:deviceTokenString forKey:DeviceTokenKey];

                [[NSUserDefaults standardUserDefaults] synchronize];

            }

    }

    }

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

    {

        LogError(@"Error in registration. Error: %@", error);

    }

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

    {

        NSLog(@"receive notification date:%@, server send notification info:%@", [NSDate date], userInfo);

        NSString * jsonStr = [userInfo objectForKey:@"acme"];

        NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        

        [[NSNotificationCenter defaultCenter] postNotificationName:CloudSeverPushNotification object:parsedData];

        

    // [[CloudDriverManager shared] startCloudSync];

    }

  • 相关阅读:
    Django视图层进阶、模板层
    【python学习笔记:Django】3.生活需要仪式感——Hello World
    【python学习笔记:Django】2.启动虚拟环境库出错——Windows PowerShell中无法加载文件 xxxScriptsActivate.ps1,因为在此系统上禁止运行脚本
    【python学习笔记:Django】1.开发环境搭建——“三剑客”:python、django、visual studio code
    【window使用技巧】如何提高某盘下载速度?
    【Python应用】爬取LOL皮肤图片(面向过程编程)
    【Python图形界面编程】:PyQt5编程入门(看这篇就够了!)
    android调用百度地图(1)新手环境搭建基础应用教程
    android实现调用科大讯飞语音识别功能详细步骤
    Genymotion模拟器下载安装到连接使用一站式教程
  • 原文地址:https://www.cnblogs.com/ouyangfang/p/3964708.html
Copyright © 2011-2022 走看看