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

ぃ、小莉子 提交于 2019-12-20 06:04:50

问题


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


回答1:


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



回答2:


use this php code http://d1xzuxjlafny7l.cloudfront.net/downloads/SimplePush.zip

or go to the this link http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1




回答3:


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();
}


来源:https://stackoverflow.com/questions/13047002/how-to-send-an-apple-mdm-push-notification-with-plain-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!