Saturday, 15 June 2013

php - Store iOS device token to a MySQL database -



php - Store iOS device token to a MySQL database -

i ready production apns, , assuming best (only, easiest?) way store device ids sign force in database (assuming sql) , having php script (somehow?) send of them (for loop?) instead of 1 (devicetoken - mine)like have now.

this have in appdelegate.m:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // allow device know want receive force notifications [[uiapplication sharedapplication] registerforremotenotificationtypes: (uiremotenotificationtypebadge | uiremotenotificationtypesound | uiremotenotificationtypealert)]; homecoming yes; } - (void)application:(uiapplication*)application didregisterforremotenotificationswithdevicetoken:(nsdata*)devicetoken { nslog(@“token: %@", devicetoken); } - (void)application:(uiapplication*)application didfailtoregisterforremotenotificationswitherror:(nserror*)error { nslog(@"failed token, error: %@", error); }

so here logging users devicetoken, , manually copied , pasted php script goes this:

<?php // set device token here (without spaces): $devicetoken = '[device token here]'; // set private key's passphrase here: $passphrase = '[passphrase here]'; // set alert message here: $message = 'hello, there!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // open connection 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) exit("failed connect: $err $errstr" . php_eol); echo 'connected apns' . php_eol; // create payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); // encode payload json $payload = json_encode($body); // build binary notification $msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload; // send server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'message not delivered' . php_eol; else echo 'message delivered' . php_eol; // close connection server fclose($fp);

so need help making connection server's database (mysql) , storing devicetoken. need loop php sending notification devices.

let me know if need clarify on anything.

thank you.

for storing device token in mysql database, must create seperate api.

for ex. calling http://example.com/savedevicetoken device token.

at time notification registration(didregisterforremotenotificationswithdevicetoken), must phone call api parameter device token.

in api can fetch device token , save database. in above code fetch device token db.

php mysql ios apple-push-notifications

No comments:

Post a Comment