iOS 遠程推送

前期準備工做:到蘋果開發者網站上註冊AppID、推送證書、描述性證書php

代碼段實現:ios

 if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
        //IOS8
        //建立UIUserNotificationSettings,並設置消息的顯示類類型
        UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
        
        [application registerUserNotificationSettings:notiSettings];
    }else{
       
        //ios7.0及如下
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                                       |UIRemoteNotificationTypeSound    |UIRemoteNotificationTypeAlert)];
    
    }

代理方法實現數據庫

//會接收來自蘋果服務器給你返回的deviceToken,而後你須要將它添加到你本地的推送服務器上。(很重要,決定你的設備能不能接收到推送消息)。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{
    //註冊成功,將deviceToken保存到應用服務器數據庫中,在應用服務器端須要
    NSLog(@"---Token--%@", pToken);
   
}
//當註冊失敗時,觸發此函數
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    
    NSLog(@"Regist fail%@",error);
}


//
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    //處理推送消息
    NSLog(@"userInfo == %@",userInfo);
    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定",nil];
    
    [alert show];
}

2.PHP服務端json

將simplepush.php這個推送腳本也放在push文件夾中服務器

    <?php  
      
    // ??????????deviceToken???????????????  
    $deviceToken = 'c95f661371b085e2517b4c12cc76293522775e5fd9bb1dea17dd80fe85583b41';  
      
    // Put your private key's passphrase here:  
    $passphrase = 'abc123';  
      
    // Put your alert message here:  
    $message = 'My first push test!';  
      
    ////////////////////////////////////////////////////////////////////////////////  
      
    $ctx = stream_context_create();  
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');  
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  
      
    // Open a connection to the APNS server  
    //??????????  
     //$fp = stream_socket_client(?ssl://gateway.push.apple.com:2195?, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);  
    //?????????????appstore??????  
    $fp = stream_socket_client(  
    'ssl://gateway.sandbox.push.apple.com:2195', $err,  
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);  
      
    if (!$fp)  
    exit("Failed to connect: $err $errstr" . PHP_EOL);  
      
    echo 'Connected to APNS' . PHP_EOL;  
      
    // Create the payload body  
    $body['aps'] = array(  
    'alert' => $message,  
    'sound' => 'default'  
    );  
      
    // Encode the payload as JSON  
    $payload = json_encode($body);  
      
    // Build the binary notification  
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;  
      
    // Send it to the server  
    $result = fwrite($fp, $msg, strlen($msg));  
      
    if (!$result)  
    echo 'Message not delivered' . PHP_EOL;  
    else  
    echo 'Message successfully delivered' . PHP_EOL;  
      
    // Close the connection to the server  
    fclose($fp);  
    ?>  

deviceToken填寫你接收到的token,passPhrase則填寫你的ck.pem設置的密碼。app

此刻就是見證奇蹟的時候了socket

使用終端進入到push文件夾,在終端輸入 php simplepush.php函數


若顯示以上提示則表示推送成功了。網站

相關文章
相關標籤/搜索