簡體   English   中英

推送通知不起作用

[英]Push Notification doesn't work

我正在開發需要推送通知的應用程序。 我正在按照本教程使用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
    //$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);
?>

當我啟動php頁面時什么也沒發生..為什么? 誰能幫我?

我認為您的令牌不正確。 嘗試:

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

這應該有助於:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM