How to send an apple mdm push notification with plain php?
Asked Answered
D

2

0

i've done a apple-mdm-ota-server for IOS so far. The devices deliver me following things to the server (in form of a plist/xml):

-Push Magic Token -Device Token (in b64 format oO) -PN-Topic (com.apple.mgmt.xxx) -UDID (in hexcode) -UnlockToken (a very long b64 encoded thing)

I need to send the following content to the push notification thing from apple:

{"mdm":"pushMagic"}

Pushmagic = the Push Magic Token

How I need to compose the data I need to write into the apns socket? I tried the one that works for other push notifications, but for this it doesn't. There is happening just nothing :-(.

$apns_url = 'gateway.push.apple.com';  

[...]

$apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port,
$error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

fwrite($apns, chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ',
'', $wellWrittenToken)) . chr(0) . chr(strlen($mdmInitialPush)) .
$mdmInitialPush);

[...]

$error is 0. $wellwrittentoken = the device-token in hex-code $apns_xx - The ssl thing seems to work, because it gave error message as it didn't $mdminitialpush - my message I want to send

Demotic answered 24/10, 2012 at 10:5 Comment(0)
P
4

I did not see that you included your APNS key when setting up the stream. Here is (roughly) what we do:

$apns_certkey_path = '/path/to/cert/and/key/file' ;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apns_certkey_path);

$apns = stream_socket_client(
  'ssl://' . $apns_url . ':' . $apns_port,
  $error,
  $errorString,
  2, // timeout
  STREAM_CLIENT_CONNECT,
  $streamContext
);

$payload = json_encode(array('mdm' => $PushMagic));
$apnsMessage = chr(0)  . chr(0)
             . chr(32) . base64_decode($ApnsTokenB64)
             . chr(0)  . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
Pork answered 24/10, 2012 at 13:11 Comment(5)
Thanks, I was also able to modify this code to get MDM notifications sent correctly after trying a couple solutions which only applied to iOS app notifications.Bondwoman
@Bondwoman Hi bobcat. What did you modify to your code to get MDM notifications correctly.Discourage
my device token without any encoding is "\xEA\u0013\xD0sa\xEFt\r\xB4\\iP\x939\xC9\xC6V\xA9\xA4*\xF3\x9By\tq\xC9c\x8C\xEB \u0017i" , do i need to decode first and then fire notification ?Illuminator
i tried do encoding/decoding and converted to bin2hex() , nothing is working :(Illuminator
could you please check this, https://mcmap.net/q/1628220/-ios-mdm-push-notification-using-php-not-working/3145189Illuminator
A
0

use this libray

function push_device($data) {
    $push = new ApnsPHP_Push(
        ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION,
        '../MyPushCert.pem'
    );
    $push->connect();
    $message = new ApnsPHP_Message_Custom($data["Token"]);
    $message->setCustomProperty('mdm', $data["PushMagic"]);
    $push->add($message);
    $push->send();
    $push->disconnect();
}
Autogenous answered 8/3, 2017 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.