iPhone推送通知不起作用

时间:2012-02-03 12:08:52

标签: php iphone apple-push-notifications

我正在我的应用程序中实现推送通知。

Server: php
Client: iphone

Sever side Coding:

function pushToIphone($deviceToken, $badge){
    ini_set('display_errors','on');
    error_reporting(E_ALL);

    //$apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsHost = 'gateway.push.apple.com';
    $apnsPort = 2195;
    $pem_path = dirname(__FILE__);  
    $pem_path = $pem_path .'\cert';
    $apnsCert = $pem_path.'\apns_cer.pem';

    echo $apnsCert."<br/>"; 
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

    if($apns) {
        echo "Connection Established<br/>";

        $payload = array();
        //$payload['aps'] = array('alert' => 'BiiMe finds product for you', 'badge' => $badge, 'sound' => 'default');
        //$payload = get_payload_message('BiiMe finds product for you',$badge);
        //$payload['server'] = array('serverId' => $serverId, 'name' => $serverName);
        //$payload = json_encode($payload);

        $payload = get_payload_message('BiiMe finds product for you',$badge);
        $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

        echo $apnsMessage."<BR/>";

        $fwrite = fwrite($apns, $apnsMessage);  
        echo $fwrite." bytes written<BR/>"; 
    } else {
        echo "Connection fail<br/>";
    }

    //socket_close($apns);  
    fclose($apns);
}

function get_payload_message($message_text,$badge,$sound='default')
{
    $PAYLOAD_MAXIMUM_SIZE = 256;

    $payload['aps'] = array("alert" => "$message_text", 'badge' => $badge, 'sound' => $sound);
    $payload = json_encode($payload);

    $nJSONPayloadLen = strlen($payload);
    if($nJSONPayloadLen > $PAYLOAD_MAXIMUM_SIZE)
    {
        $nTextLen = strlen($message_text);
        if($nJSONPayloadLen - $nTextLen <= $PAYLOAD_MAXIMUM_SIZE)
        {
            $badge_count = substr($message_text, 0, $nTextLen - ($nJSONPayloadLen - $PAYLOAD_MAXIMUM_SIZE));
            $payload  = get_payload_message($message_text);
        }
    }
    return  $payload;
}
我补充说:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 


    NSLog(@"It comes<<<<<<<<<<<<<---------------------------------------");
    NSString *str = [NSString 
                     stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(str);
    NSString *token = [NSString stringWithFormat:deviceToken];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:token forKey:@"deviceId"];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    NSLog(@"It comes with error---------------------------->>>>>>>>>>>>>>");
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(str);    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:str forKey:@"deviceId"];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }    

}

我也用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    UIImageView *loading = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
    [loading setImage:[UIImage imageNamed:@"Default.png"]];
    [window addSubview:loading];
    [loading release];

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];
    }

这是我使用的完整代码:

设备连接时显示iPhone日志:

2012-02-03 19:51:32.872 BiiMe[7544:707] It comes<<<<<<<<<<<<<---------------------------------------
2012-02-03 19:51:32.873 BiiMe[7544:707] Device Token=<26d906c5 c273446d 5f40d2c1 73ddd3f6 869b2666 b1c7afd5 173d69b6 629def70>

所有服务器端:  已建立连接

我缺少什么..?或者我应该在我的代码中实现什么:

你的回答对我有帮助。

2 个答案:

答案 0 :(得分:2)

gateway.push.apple.com用于已发布的应用程序或AdHoc应用程序。如果您使用开发配置文件进行检查,则可能需要将其更改为gateway.sandbox.push.apple.com

希望这有帮助。

答案 1 :(得分:2)

在AppDelegete

中的didFinishLaunchingWithOption中使用此代码
[[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                         UIRemoteNotificationTypeSound |
                                         UIRemoteNotificationTypeAlert)];