Apple Remote Push Notifications

一、幫助文檔參考:php

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

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

二、服務器證書文件生成文檔:ios

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

三、APNS全流程:json

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

四、Google描述APNS:服務器

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

五、APNs之我見:iphone

整個流程中的角色: 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須要知道將通知發送到哪一個地址

六、實際操做流程

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];

}

相關文章
相關標籤/搜索