Sending a push notification from my provider (PHP) server to an iOS device
I'm currently in the process of setting up a provider server to send out push notifications to iOS devices with my app installed.
I'm struggling to find any recent versions of working examples but have put the following together. I'm getting no errors returned but the push notification isn't showing up on my test device.
I can confirm the latest device token is being used and the .pem cert is valid.
Any help on this would be greatly appreciated.
Best regards,
J
$apnsHost = 'api.sandbox.push.apple.com';
$apnsCert = '/cert.pem';
$apnsPort = 2197;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array(
'alert' => array(
'title' => 'Sample push number ' . mt_rand(100, 999),
'body' => 'Sample push body goes here'
),
'badge' => 9,
'sound' => 'default',
'my_value_1' => 'more data',
'my_value_2' => 'even more data'
);
// JSON-encode payload
$output = json_encode($payload, JSON_NUMERIC_CHECK);
// Cancel if payload too long
if(strlen($output) > 2048){
die('Payload too long ('.strlen($output).'): Aborted');
}
// Build the binary notification (correctly)
$token = '185e522cd0e3f35f09sdg398f25fe28038f6a0743339b0a378';
$apnsMessage = chr(0).pack('n', 32).$token.pack('n', strlen($output)).$output;
// Send it to the server
$result = fwrite($apns, $apnsMessage, strlen($apnsMessage));
// Result returns number of bytes sent on success, false otherwise
if(intval($result) > 0){
// Push sent
$response = $result.' bytes sent. $payload size is: '.strlen($output).' (payload limit is 2048 bytes)';
fclose($apns);
}else{
// Push failed
$error = error_get_last();
$response = (is_array($error) ? $error['message'] : 'undefined error');
}
// Show result
die($response);
Output: 240 bytes sent. $payload size is: 171 (payload limit is 2048 bytes)
php ios apple-push-notifications
add a comment |
I'm currently in the process of setting up a provider server to send out push notifications to iOS devices with my app installed.
I'm struggling to find any recent versions of working examples but have put the following together. I'm getting no errors returned but the push notification isn't showing up on my test device.
I can confirm the latest device token is being used and the .pem cert is valid.
Any help on this would be greatly appreciated.
Best regards,
J
$apnsHost = 'api.sandbox.push.apple.com';
$apnsCert = '/cert.pem';
$apnsPort = 2197;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array(
'alert' => array(
'title' => 'Sample push number ' . mt_rand(100, 999),
'body' => 'Sample push body goes here'
),
'badge' => 9,
'sound' => 'default',
'my_value_1' => 'more data',
'my_value_2' => 'even more data'
);
// JSON-encode payload
$output = json_encode($payload, JSON_NUMERIC_CHECK);
// Cancel if payload too long
if(strlen($output) > 2048){
die('Payload too long ('.strlen($output).'): Aborted');
}
// Build the binary notification (correctly)
$token = '185e522cd0e3f35f09sdg398f25fe28038f6a0743339b0a378';
$apnsMessage = chr(0).pack('n', 32).$token.pack('n', strlen($output)).$output;
// Send it to the server
$result = fwrite($apns, $apnsMessage, strlen($apnsMessage));
// Result returns number of bytes sent on success, false otherwise
if(intval($result) > 0){
// Push sent
$response = $result.' bytes sent. $payload size is: '.strlen($output).' (payload limit is 2048 bytes)';
fclose($apns);
}else{
// Push failed
$error = error_get_last();
$response = (is_array($error) ? $error['message'] : 'undefined error');
}
// Show result
die($response);
Output: 240 bytes sent. $payload size is: 171 (payload limit is 2048 bytes)
php ios apple-push-notifications
add a comment |
I'm currently in the process of setting up a provider server to send out push notifications to iOS devices with my app installed.
I'm struggling to find any recent versions of working examples but have put the following together. I'm getting no errors returned but the push notification isn't showing up on my test device.
I can confirm the latest device token is being used and the .pem cert is valid.
Any help on this would be greatly appreciated.
Best regards,
J
$apnsHost = 'api.sandbox.push.apple.com';
$apnsCert = '/cert.pem';
$apnsPort = 2197;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array(
'alert' => array(
'title' => 'Sample push number ' . mt_rand(100, 999),
'body' => 'Sample push body goes here'
),
'badge' => 9,
'sound' => 'default',
'my_value_1' => 'more data',
'my_value_2' => 'even more data'
);
// JSON-encode payload
$output = json_encode($payload, JSON_NUMERIC_CHECK);
// Cancel if payload too long
if(strlen($output) > 2048){
die('Payload too long ('.strlen($output).'): Aborted');
}
// Build the binary notification (correctly)
$token = '185e522cd0e3f35f09sdg398f25fe28038f6a0743339b0a378';
$apnsMessage = chr(0).pack('n', 32).$token.pack('n', strlen($output)).$output;
// Send it to the server
$result = fwrite($apns, $apnsMessage, strlen($apnsMessage));
// Result returns number of bytes sent on success, false otherwise
if(intval($result) > 0){
// Push sent
$response = $result.' bytes sent. $payload size is: '.strlen($output).' (payload limit is 2048 bytes)';
fclose($apns);
}else{
// Push failed
$error = error_get_last();
$response = (is_array($error) ? $error['message'] : 'undefined error');
}
// Show result
die($response);
Output: 240 bytes sent. $payload size is: 171 (payload limit is 2048 bytes)
php ios apple-push-notifications
I'm currently in the process of setting up a provider server to send out push notifications to iOS devices with my app installed.
I'm struggling to find any recent versions of working examples but have put the following together. I'm getting no errors returned but the push notification isn't showing up on my test device.
I can confirm the latest device token is being used and the .pem cert is valid.
Any help on this would be greatly appreciated.
Best regards,
J
$apnsHost = 'api.sandbox.push.apple.com';
$apnsCert = '/cert.pem';
$apnsPort = 2197;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array(
'alert' => array(
'title' => 'Sample push number ' . mt_rand(100, 999),
'body' => 'Sample push body goes here'
),
'badge' => 9,
'sound' => 'default',
'my_value_1' => 'more data',
'my_value_2' => 'even more data'
);
// JSON-encode payload
$output = json_encode($payload, JSON_NUMERIC_CHECK);
// Cancel if payload too long
if(strlen($output) > 2048){
die('Payload too long ('.strlen($output).'): Aborted');
}
// Build the binary notification (correctly)
$token = '185e522cd0e3f35f09sdg398f25fe28038f6a0743339b0a378';
$apnsMessage = chr(0).pack('n', 32).$token.pack('n', strlen($output)).$output;
// Send it to the server
$result = fwrite($apns, $apnsMessage, strlen($apnsMessage));
// Result returns number of bytes sent on success, false otherwise
if(intval($result) > 0){
// Push sent
$response = $result.' bytes sent. $payload size is: '.strlen($output).' (payload limit is 2048 bytes)';
fclose($apns);
}else{
// Push failed
$error = error_get_last();
$response = (is_array($error) ? $error['message'] : 'undefined error');
}
// Show result
die($response);
Output: 240 bytes sent. $payload size is: 171 (payload limit is 2048 bytes)
php ios apple-push-notifications
php ios apple-push-notifications
asked Dec 31 '18 at 21:21
JamLisJamLis
180114
180114
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991504%2fsending-a-push-notification-from-my-provider-php-server-to-an-ios-device%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991504%2fsending-a-push-notification-from-my-provider-php-server-to-an-ios-device%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown