iOS 8 Apple Push Notification Service

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   os   ar   for   

  • Apple Configuration

    • 1. create Apple ID in Apple Developer Website
    • 2. check on "Push Notification" in the functionality
    • 3. configure for Push Notification in Development: create certificate
      • 3.1 upload the certificate file which is generated by keychain access. 
        • how to generating the Certificate Signing Request (CSR): 
        • 3.1.1 keychain access -> certificate assistant -> request a certificate from a certificate authority
        • 3.1.2 save the certificate file to the disk as "your name.certSigningRequest", which will be used to be uploaded.
      • 3.2 download SSL Certificate and save as “aps_development.cer”
      • 3.3 click the “aps_development.cer” to install, you will see it in keychain access, export as .p12 file
      • 3.4 use command line to transform it from .p12 to .pem, which will be used in the server. keep in mind for the password, which will also be used in the server
        • $ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts
    • 4. create provision file for this Apple ID: generate and drag it to Xcode

  • Xcode Side

In AppDelegate.m, implement the methods below:

//---------------------------------------------#pragma mark - App Delegate//---------------------------------------------- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        //---------------------------------------------    // set push notification    //---------------------------------------------    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])    {        // iOS 8 Notifications        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];                [application registerForRemoteNotifications];    }    else    {        // iOS < 8 Notifications        [application registerForRemoteNotificationTypes:         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];    }        //---------------------------------------------    //Accept push notification when app is not open    //---------------------------------------------    NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];    if (remoteNotification) {        //        [self handleRemoteNotification:application userInfo:remoteNotif];    }    else{        // do nothing ..    }        return YES;}

//---------------------------------------------#pragma mark - Push Notifications Delegate//---------------------------------------------- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{    NSLog(@"This device token is: %@", deviceToken);    NSString *token = [[[[deviceToken description]                         stringByReplacingOccurrencesOfString: @"<" withString: @""]                        stringByReplacingOccurrencesOfString: @">" withString: @""]                       stringByReplacingOccurrencesOfString: @" " withString: @""];        // call the server function to register this device    // ..}- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{    NSLog(@"Failed to get token, error: %@", error);}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    NSLog(@"didReceiveRemoteNotification");        UIApplicationState applicationState = application.applicationState;    if ( applicationState == UIApplicationStateActive ){        // app was already in the foreground        NSLog(@"Content: %@", [[userInfo objectForKey: @"aps"] objectForKey: @"alert"]);                UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];        if (enabledTypes & UIRemoteNotificationTypeBadge) {            //badge number is enabled        }        if (enabledTypes & UIRemoteNotificationTypeSound) {            //sound is enabled        }        if (enabledTypes & UIRemoteNotificationTypeAlert) {            //alert msg is enabled        }    }    else{        // app was just brought from background to foreground    }}


  • Server Side(Ruby on Rails):

Gem Recomend: https://github.com/nomad/Houston


  • Reference:

[1]http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

[2]https://www.youtube.com/watch?v=_3YlqWWnI6s

iOS 8 Apple Push Notification Service

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.