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被正確地到達。一個簡單的不合格證書或缺乏證書可能會讓您離開工作日。個人經驗。

相關問題