5

我在 iOS/android 中创建应用程序。当设备中收到远程通知时,didReceiveRemoteNotification应该被调用。但它没有发生。我通过 APNS 发送消息的服务器端代码如下:

$deviceToken = $obj_listener->ref_id;

// Put your private key's passphrase here:
$passphrase = 'blahblah';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/var/www/mobileapp/TestAppCK.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
                           'ssl://gateway.sandbox.push.apple.com:2195', $err,
                           $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
    $this->log->debug("Failed to connect: $err $errstr" . PHP_EOL);
    exit("Failed to connect: $err $errstr" . PHP_EOL);
}

$badge_count = $obj_listener->badge_count + 1;

// Create the payload body
$body['aps'] = array(                  
                    //'alert' => 'Message received',
                    'sound' => 'default',
                    'badge' => $badge_count,
                    'msg_id' => $this->msg_id,
                    //'user_key' => $obj_listener->ref_id,
                    'email' => $obj_listener->to_email_id
                );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

// Close the connection to the server
fclose($fp);

我的 Objective-C 端代码如下:

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

    //UIWebView *NewWebView = NULL;
    NSLog(@"didReceiveRemoteNotification function");

    return;
}

我已经在服务器端代码中检查了设备令牌。这对设备是正确的。为什么上面的函数没有被调用。提前致谢。

4

2 回答 2

1

如果您没有在应用启动时注册通知,则永远不会收到通知。

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

现在,关于您的服务器端,我建议您在调试模式下运行您的代码,看看是否可以通过 SSL 正确访问 Apple 的网关。一个简单的坏证书或缺少它可能会让你失去几天的工作。个人经验。

于 2013-06-06T08:06:31.500 回答
0

如果应用程序不在后台,您应该使用以下代码

   //-------------- check notification when app is come to foreground after apllication get terminated ----------------//

UILocalNotification *localNotif =

[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (localNotif) {

    [self handleRemotNotification:[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]]; // private method


}
于 2013-06-06T07:03:59.770 回答