5

Tôi đang tạo ứng dụng trong iOS/android. Khi nhận được thông báo từ xa trong thiết bị, didReceiveRemoteNotification sẽ được gọi. Nhưng nó không xảy ra. mã phía máy chủ của tôi để gửi msg qua APNS là như dưới:didReceiveRemoteNotification không được gọi

$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); 

My Objective-C mã phía là như dưới:

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

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

    return; 
} 

Tôi đã kiểm tra cho các thẻ điện thoại ở các mã phía máy chủ. Nó là chính xác cho thiết bị. Tại sao chức năng trên không được gọi. Cảm ơn trước.

+1

Kiểm tra cài đặt iPhone của bạn, nếu thông báo đẩy là BẬT cho ứng dụng của bạn –

+0

Ngoài ra, hãy đảm bảo bạn đang sử dụng cấu hình cấp phép chính xác (được liên kết với chứng chỉ mà bạn đã sử dụng trong mã máy chủ) –

+0

tất cả đều tiếp cận thiết bị. – clint

Trả lời

0

Nếu ứng dụng không có trong nền bạn nên sử dụng đoạn mã sau

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

Nếu bạn không đăng ký thông báo khi khởi động ứng dụng của bạn, họ sẽ không bao giờ được nhận.

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

Bây giờ, về phía máy chủ của bạn, tôi đề nghị bạn chạy mã trong chế độ gỡ lỗi và xem cổng của Apple có được truy cập thông qua SSL hay không. Một chứng chỉ xấu đơn giản hoặc thiếu nó có thể đưa bạn đi xa ngày làm việc. Kinh nghiệm cá nhân.

Các vấn đề liên quan