2011-12-30 10 views
0

푸시 알림이 필요한 앱을 개발하고 있습니다. 나는 php로 푸시 알림을 구현하기 위해 다음과 같은 tutorial을 따라갈 것이다. 그래서 프로덕션 인증서를 사용하고 있습니다. ... 왜 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); 
?> 

아무것도 일어나지 : 이것은 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 페이지입니까? 누가 날 도울 수 있죠?

답변

0

당신의 토큰이 정확하지 않다고 생각합니다. 시도 :

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

을 토큰이 À aÂÑ5ΔC≤¸u처럼 .. 맞습니까? – JackTurky

+0

PHP 코드에 아무 것도 일어나지 않으면 UbQNâò-ù € xxxxx aÂÑ5ΔC≤¸u 추가 : ( – JackTurky

+0

) NSString * token = [[NSString alloc] initWithBytes : [deviceToken bytes] length : deviceToken.length encoding : NSASCIIStringEncoding] ; – CarlJ

0

이는 데 도움이 될 것입니다 이런 식으로 nslog 인쇄 뭔가

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

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

so) NSString * tok = [[NSString alloc] 토큰은 공백이 있거나 < >이 맞아야합니까? 시도하지만 PHP 코드를 실행하면 갈 수 없습니다. (아무런 알림도 수신되지 않습니다.) – JackTurky

+0

1. 예, < > .so가 없으므로. 당신은 APN의 인증서입니까? PHP 코드? – CarlJ

관련 문제