didReceiveRemoteNotification方法没有被调用

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

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


1
请在您的iPhone设置中检查,确认您的应用程序推送通知功能已开启。 - P.J
此外,请确保您正在使用正确的预配文件(与您在服务器代码中使用的证书相关联)。 - Nikita P
它根本没有到达设备。 - clint
你是否发现它根本没有被调用,或者只有在应用程序未运行时才会被调用? 如果应用程序在前台或后台运行,我可以调用它,但是如果通知在应用程序未运行时到达,则无法调用它。 - jaseelder
2个回答

1
如果您在应用程序启动时不注册通知,那么您将永远无法收到通知。
 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];

现在,关于您的服务器端,我建议您以调试模式运行您的代码,并查看是否通过SSL成功到达了苹果网关。一个简单的错误证书或缺失可能会浪费您几天的工作时间。这是我的个人经历。


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


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接