Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didReceiveRemoteNotification not getting invoked

I am creating app in iOS/android. When a remote notification is received in the device, didReceiveRemoteNotification should get called. But it is not happening. My server side code for sending msg through APNS is as under:

$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 side code is as under:

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

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

    return;
}

I have checked for the device token in the server side code. It is correct for the device. Why is the above function not getting called. Thanks in advance.

like image 837
clint Avatar asked Nov 12 '22 14:11

clint


1 Answers

If you don't register for notifications at your app start, they will never be received.

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

Now, about your server side, I suggest you run your code in debug mode and see if Apple's gateway is being properly reached through SSL. A simple bad cert or lack of it can take you away days of work. Personal experience.

like image 180
John Doe Avatar answered Nov 15 '22 05:11

John Doe