2011-12-30 188 views
0

我正在開發需要推送通知的應用程序。 我按照這個tutorial來實現推送通知與PHP。 所以我使用生產證書。 這是applicationDelegate代碼:推送通知不起作用

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    const unsigned* tokenBytes = [deviceToken bytes]; 
    NSString* tok = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", 
           ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]), 
           ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]), 
           ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; 
    NSLog([NSString stringWithFormat:@"token 1 = %@",tok]); 
    [[NSUserDefaults standardUserDefaults] setObject:tok forKey:@"token"]; 
} 

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
} 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    NSLog(@"Received notification: %@", userInfo); 
} 

,這是服務器端的PHP頁面:當我啓動的PHP頁面

<?php 
    //$token = $_GET['t']; 
    $token = "xxxxxxxxxxx....xxxxxx"; 
    $who =$_GET['c']; 
    $notification = $_GET['n']; 
    $message = 'Hello'; 
    $badge = 3; 
    $sound = 'default'; 
    $payload = array(); 
    $payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound); 
    $payload = json_encode($payload); 
    $apns_url = NULL;  
    $apns_cert = NULL; 
    $apns_port = 2195; 
$apns_url = 'gateway.push.apple.com'; 
$apns_cert = 'cert-prod.pem'; 

    $stream_context = stream_context_create(); 
    stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert); 
    $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context); 
    $device_tokens = array(); 
    $device_tokens[0] = $token; 
    foreach($device_tokens as $key=>$device_token) 
    { 
     $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload; 
     fwrite($apns, $apns_message); 
    } 
    @socket_close($apns); 
    @fclose($apns); 
?> 

沒有發生..爲什麼?誰能幫我?

回答

0

我認爲你的令牌不正確。嘗試:

NSString *token = [NSString stringWithCString:[deviceToken bytes]]; 
+0

這樣的aÂÑ5ΔC≤¸u令牌..是正確的? – JackTurky 2011-12-30 13:31:49

+0

添加UbQNâo-ù€xxxxxaÂÑ5ΔC≤¸u到PHP代碼沒有任何事情發生:( – JackTurky 2011-12-30 13:33:21

+0

不看起來很好嘗試:NSString *標記= [[NSString分配] initWithBytes:[deviceToken字節]長度:deviceToken.length編碼:NSASCIIStringEncoding] ; – CarlJ 2011-12-30 13:45:47

0

這應該幫助:這樣的NSLog打印的東西

NSString *deviceTokenStr = [[[[deviceToken description] 
           stringByReplacingOccurrencesOfString: @"<" withString: @""] 
           stringByReplacingOccurrencesOfString: @">" withString: @""] 
           stringByReplacingOccurrencesOfString: @" " withString: @""]; 

    NSLog(@"Device Token: %@", deviceTokenStr); 
+0

so令牌必須與任何空格或< >是真的?我嘗試,但是當我啓動php代碼它不會去:(沒有通知到達 – JackTurky 2011-12-30 14:50:19

+0

1.是的,沒有< > .so現在我可以出來它。你是APN的證書PHP代碼? – CarlJ 2011-12-30 14:53:12