iPhone 推送通知不起作用

发布于 2025-01-01 22:52:05 字数 4549 浏览 0 评论 0原文

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

Server: php
Client: iphone

服务器端编码:

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;
}

Iphone 端我添加:

- (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>

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

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

你的答案会对我有帮助。

I am implementing a push notification in my application.

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;
}

Iphone side i added:

- (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]);
    }    

}

also i use:

- (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)];
    }

This is my whole code that used:

iPhone Log shows while Device is Connected:

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>

all times server side:
Connection Established

What is missing by me..? or what should i implemented in my code:

your answer will help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2025-01-08 22:52:05

在 AppDelegete 的 didFinishLaunchingWithOption 中使用此代码

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

use this code in didFinishLaunchingWithOption in AppDelegete

[[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                         UIRemoteNotificationTypeSound |
                                         UIRemoteNotificationTypeAlert)];
把昨日还给我 2025-01-08 22:52:05

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

希望这会有所帮助。

gateway.push.apple.com is used for released application or AdHoc application. If you are checking with development profile you might need to change it to gateway.sandbox.push.apple.com

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文