iOS 推播通知流程

來源:互聯網
上載者:User

標籤:style   http   java   os   io   檔案   for   ar   

別的不說,現在AppDelegate.m中添加以下代碼塊

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 將這行代碼插入    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{    NSLog(@"deviceToken = %@", deviceToken);}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{    NSLog(@"Error in registration. Error: %@", error);}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{    NSDictionary *dic = [userInfo objectForKey:@"aps"];    NSLog(@"收到的推送訊息%@", [dic objectForKey:@"alert"]);    if (nil != [dic objectForKey:@"alert"]) {        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"推播通知" message:[dic objectForKey:@"alert"] delegate:self cancelButtonTitle:@"關閉" otherButtonTitles:@"更新狀態", nil];        [alterView show];        [alterView release];    }}

將列印出來的deviceToken 去掉空格和<>符號,產生一個許可證號留著

======================================================

產生測試認證:

 1、進入Provisioning Portal, 下載Certificates在development下的認證

 2、 找到需要測試的app id,然後enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要輸入1)中的簽署憑證才可以產生一個aps_developer_identity.cer.

 3、雙擊aps_developer_identity.cer,會開啟系統的key chain. 在My certificates下找到Apple Development Push Services。需要為certificate和它之下的private key (點前面的箭頭,會出現一個鑰匙) 各自export出一個.p12檔案。(會出現設定密碼過程)

 4、將上面的2個.p12檔案轉成.pem格式:

       openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12   

       openssl pkcs12 -nocerts -out key.pem -in key.p12  

 5、如果需要對 key不進行加密:

        openssl rsa -in key.pem -out key.unencrypted.pem  

 6、合并兩個.pem檔案, 這個ck.pem就是服務端需要的認證了。

       cat cert.pem key.unencrypted.pem > ck.pem  

================================================================

用php寫伺服器 send.php

#!/usr/bin/php<?php$deviceToken = ‘3cec682c9b773f2d82e337a98c35483aaa4f4c74e3933f9cb57e9d4c2f17f52d‘; // masked for security reason// Passphrase for the private key (ck.pem file)$pass = ‘‘;// Get the parameters from http get or from command line$message = $_GET[‘message‘] or $message = $argv[1] or $message = ‘Message received from javacom‘;$badge = (int)$_GET[‘badge‘] or $badge = (int)$argv[2];$sound = $_GET[‘sound‘] or $sound = $argv[3];$message = ‘Hello qwe‘;$badge = 1;$sound = ‘cowbell.wav‘;// Construct the notification payload$body = array();$body[‘aps‘] = array(‘alert‘ => $message);if ($badge)$body[‘aps‘][‘badge‘] = $badge;if ($sound)$body[‘aps‘][‘sound‘] = $sound;/* End of Configurable Items */$ctx = stream_context_create();stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘);// assume the private key passphase was removed.// stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);$fp = stream_socket_client(‘ssl://gateway.sandbox.push.apple.com:2195‘, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);if (!$fp) {print "Failed to connect $err $errstrn";return;}else {print "Connection OK\n";}$payload = json_encode($body);$msg = chr(0) . pack("n",32) . pack(‘H*‘, str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload;print "sending message :" . $payload . "\n";fwrite($fp, $msg);fclose($fp);?>

把php檔案與ck.pem放到同一檔案夾下。

在命令列中,php send.php 即可測試


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.