5

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

$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设置 –

+0

另请确保您使用的是正确的设置配置文件(链接到您将在服务器代码中使用的证书)。 –

+0

它不在全部到达设备。 – clint

回答

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 


} 
1

如果你没有在你的应用程序开始通知报名,他们将永远不会被接受。现在

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

,你的服务器端,我建议你在调试模式下运行代码,看看苹果公司的网关通过SSL被正确地到达。一个简单的不合格证书或缺乏证书可能会让您离开工作日。个人经验。

相关问题