首先是申請證書的網址
https://developer.apple.com/ios/manage/overview/index.action
登錄成功以後,進入iOS 配置管理的 主頁面.
第一步操作是去創建一個新的App IDs創建成功後,會需要提供安全證書來激動推送服務,如下圖:
選擇存儲到磁盤以後,生成一個文件名稱爲(簡稱CSR): CertificateSigningRequest.certSigningRequest
回到Apple頁面 將這個提交併提示激動成功.
激活成功後的App IDs 提供下載開發版或是發佈版的主動推送證書(aps_development.cer),如果需要做服務器方面的主動推送的話,就必須要下載這個文件來使用推送服務了.
第二步要爲App提供接受推送許可的證書,點擊Provisioning進行設置,添加一個新的許可,選擇剛剛新創建的App IDs. 再選擇可以調試的iPhone 設備.
最後,同樣是下載下來: YsyPushMessageDemo.mobileprovision雙擊該證書,讓其加載一次.
接下來,進入iOS工程,選擇使用該證書來調試.
紅圈中,全部設置剛剛加載的許可證書.
那麼到這裏,關於證書類的準備工作,已經全部準備就緒.
在這裏再此強調一次,每個文件的具體作用
1: CertificateSigningRequest.certSigningRequest : 爲生成App IDs 而用
2: aps_development.cer 爲開發主動推送服務而用到的證書
3: YsyPushMessageDemo.mobileprovision 爲App 接受推送通知的許可服務
主動推送的Push 代碼及使用,請參考一開始介紹的第一篇博客
這裏只附上下載地址: https://github.com/stefanhafeneger/PushMeBaby
接下來,說說收到推送通知代碼方面.
1:申請本App需要接受來自服務商提供推送消息,
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
2:申請發出後,如果成功,委託會自動返回一個設備令牌(toKen),如果失敗,將會進入另外一個失敗的委託
- //遠程通知註冊成功委託
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- NSLog(@"%@",deviceToken);
- self.viewController.toKenValueTextView.text = [NSString stringWithFormat:@"%@",deviceToken];
- self.viewController.pushStatusLabel.text = @"已經註冊.";
- }
- //遠程通知註冊失敗委託
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
- self.viewController.toKenValueTextView.text = [error description];
- }
3:將設備令牌碼提供給服務商,以供推送到具體的手機上面. 如果遠程推送消息來了,用戶點擊了推送消息,或者應用已經處於打開狀態,系統都會自動調用以下委託:
- //點擊某條遠程通知時調用的委託 如果界面處於打開狀態,那麼此委託會直接響應
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- NSLog(@"遠程通知");
- [self PMD_uesPushMessage:userInfo];
- }
4: 第三點裏面的介紹的情況是應用程序已經處於運行狀態,上面的委託纔會被執行,如果應用程序處於未啓用狀態,此時又需要響應消息,那麼需要以下委託處理.
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //這裏處理應用程序如果沒有啓動,但是是通過通知消息打開的,此時可以獲取到消息.
- if (launchOptions != nil) {
- NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- [self PMD_uesPushMessage:userInfo];
- }
- return YES;
- }
5:清空通知中心已有的推送消息,只需要將指定App 的 Badge 設置爲 0即可
- [[UIApplication sharedApplication ] setApplicationIconBadgeNumber:0];
6:主動推送的字符串必須符合如下Json數組的格式,才能正確推送到手機當中.
@"{
//自定義參數
"userinfo":
{
"name":"remote notice"
},
//標準寫法
"aps":
{
"alert":
{
"action-loc-key":"Open",//支持多語言
"body":"messgae content"//消息正文
},
"badge":1,//爲App 的icon 標記 具體數值
"sound":"default" //播放的音頻文件,default 表示系統默認的選擇列鈴聲
}
}";
到這裏就差不多結束了.
php服務器推送消息
- <?php
- $deviceToken = '38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b'; // 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];
- // Construct the notification payload
- $body = array();
- $body['aps'] = array('alert' => $message);
- if ($badge)
- $body['aps']['badge'] = $badge;
- if ($sound)
- $body['aps']['sound'] = $sound;
- $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 OKn";
- }
- $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);
- ?>