Question

In development mode I can send push notification to devices, instead I can't in production, my setup is:

--> I have downloaded aps_production.cer certificates from provisioning portal and then double click the aps_production.cer installed keychain access finally get the private key from keychain access at that time it asked password i have given some password like "admin".

--> openssl pkcs12 -in CertificateName_pro.p12 -out CertificateName_pro.pem -nodes

--> apple server apn: 'gateway.push.apple.com' with 2195 port

-->The problem is that my rails application sends notifications and didn't receive any errors, but nothing arrived on devices.

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
           NSString *deviceTokenStr = [[[[deviceToken description]
                                          stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                         stringByReplacingOccurrencesOfString: @">" withString: @""]
                                        stringByReplacingOccurrencesOfString: @" " withString: @""];

    }

simplepush.php

<?php

// Put your device token here (without spaces):
$deviceToken = 'deviceTokenStr';

// Put your private key's passphrase here:
$passphrase = 'admin';

// Put your alert message here:
$message = 'You have a new issue of admin!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'CertificateName_pro.pem');
stream_context_set_option($ctx, 'ssl', 'admin', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
                           'ssl://gateway.push.apple.com:2195', $err,
                           $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
                     'alert' => $message,
                     'sound' => 'default',
                     'badge'
                     );

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

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

While run on terminal

Mac:test macmini$ php simplepush.php

Connected to APNS

Message successfully delivered

Was it helpful?

Solution

Please be aware of the device token-ID,

which is different for the development && Production environment .

    - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
           NSString *deviceTokenStr = [[[[deviceToken description]
                                          stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                         stringByReplacingOccurrencesOfString: @">" withString: @""]
                                        stringByReplacingOccurrencesOfString: @" " withString: @""];
           UIAlertView *alert= [[UIAlertView alloc]initWithTitle:deviceTokenStr message:Nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top