I'm pretty new when it comes to this server-side stuff, and I've only just been making apps for a few months. I have my push notifications set up through the server, and everything is working fine when I want to send to an individual device. I'm using a PHP script from the SimplePush tutorial by Ray Wenderlich where I put in the device token manually for each push.
What I need to do is figure out how to write the token to my SQL database, retrieve it, and push the notifications in a loop for each token received by the server. Right now, my code to send the token to the server is something I grabbed from stackoverflow and I don't think it will work for what I want to do. I get my token and I get the message that the token was sent successfully to the URL, but I don't get anything written to my server. I put this into a new class called DataUpdater and set all of that up. It thinks it's working.
I'm pretty sure the above is useless for what I'm trying to do. Do I need to use PHP here? How do I store the token in the database? How do I retrieve it? How do I set up the notification script to grab the information and use it for each individual token?
My PHP looks like this:
What I need to do is figure out how to write the token to my SQL database, retrieve it, and push the notifications in a loop for each token received by the server. Right now, my code to send the token to the server is something I grabbed from stackoverflow and I don't think it will work for what I want to do. I get my token and I get the message that the token was sent successfully to the URL, but I don't get anything written to my server. I put this into a new class called DataUpdater and set all of that up. It thinks it's working.
Code:
+ (void)sendUserToken {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"apnsTokenSentSuccessfully"]) {
NSLog(@"apnsTokenSentSuccessfully already");
return;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://PATH TO SERVER/appdev/filecreate.php?token=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"apnsToken"]]]; //set here your URL
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error == nil) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"apnsTokenSentSuccessfully"];
NSLog(@"Token has been sent successfully");
//you can check server response here if you need
}
}];
}
I'm pretty sure the above is useless for what I'm trying to do. Do I need to use PHP here? How do I store the token in the database? How do I retrieve it? How do I set up the notification script to grab the information and use it for each individual token?
My PHP looks like this:
Code:
<?php
// Put your device token here (without spaces):
$deviceToken = 'xxxxx';
// Put your private key's passphrase here:
$passphrase = 'push';
// Put your alert message here:
$message = "There's a new sale!";
$sound = "EEPush.wav";
$badge = '1';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/noahsias/appdev/SimplePush/ck.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)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'badge' => intval($badge),
'sound' => $sound
);
// 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);