iOS MDM push notification using Php, not working

前端 未结 1 798
后悔当初
后悔当初 2020-12-06 15:37

i know it\'s duplicate of this, but this one is not working for me.

My Php code to send notification is

// Put your device token here (without spac         


        
相关标签:
1条回答
  • 2020-12-06 16:17

    I've had a go at pushing notifications myself, using PHP on Windows. One of the things I've noticed that might be wrong is your inclusion of the topic in the push payload. This isn't required.

    Here is some PHP code I've written to test using a device I registered in TestMDM. I'm not a PHP developer, but using this (http://codular.com/sending-ios-push-notifications-with-php) as a baseline and taking the pushMagic and deviceToken strings from my TestMDM database, I got it to successfully send a push.

    As I'm on Windows, I also use a PFX certificate for push.

    $deviceToken = '<YOUR DEVICE TOKEN AS BASE64 STRING>'; #base64 encoded
    $token = bin2hex(base64_decode($deviceToken));
    
    // Put your private key's passphrase here:
    $passphrase = '<YOUR PASSWORD>';
    
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', './Push.pfx');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    
    $fp = stream_socket_client('gateway.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 . "<br />";
    
    $payload = '{ \'mdm\' = \'<YOUR PUSH MAGIC FOR THIS DEVICE>\' }';
    
    $inner = chr(1) . pack('n', 32) . pack('H*', $token)
            . chr(2) . pack('n', strlen($payload)). $payload
            . chr(3) . pack('n', 4) . pack('N', 1)
            . chr(4) . pack('n', 4)
            . pack('N', time() + 86400)
            . chr(5) . pack('n', 1) . chr(10);
    
    $notification = chr(2) . pack('N', strlen($inner)) . $inner;
    
    echo $payload . PHP_EOL;
    
    $result = fwrite($fp, $notification, strlen($notification));
    
    echo $result;
    

    Once I run the script, I can see this in the logs of my device, which indicates it is working:

    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: mdmd starting...
    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: Network reachability has changed.
    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: Network reachability has changed.
    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: Push token received.
    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: Received push notification.
    Jul 11 10:16:10 Toms-iPod mdmd[11218] <Notice>: (Note ) MDM: Polling MDM server https://testmdm.azurewebsites.net/<redacted> for next command.
    

    Hopefully this will help.

    T

    0 讨论(0)
提交回复
热议问题