1

我知道它是这个的副本,但是这个对我不起作用。

我发送通知的 PHP 代码是

// Put your device token here (without spaces):
$deviceToken = '6hPQc2HvdA20XGlQkznJxlappCrzm3kJccljjOsgF2k=\n'; #base64 encoded
// Put your private key's passphrase here:
$passphrase = 'Temp1234';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', './Certificates_push_dev_new.pem');
stream_context_set_option($ctx, 'ssl', 'cafile', './entrust_2048_ca.cer');

stream_context_set_option($ctx, 'ssl', 'verify_peer', true);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://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;
// Create the payload body

$payload = json_encode(array(
                             'mdm' => '3742A63A-5BB3-4D61-9D4B-E7968DADAF84',
                             'topic' => 'com.apple.mgmt.External.e5814e69-1c24-437f-bae9-562049fd3cd5'
                             ));

echo $payload . PHP_EOL;
// Build the binary notification
#$msg = chr(0).pack('n', 32).$deviceToken . pack('n', strlen($payload)).$payload;

// Build the binary notification
#$msg = chr(0) . pack('n', 32) . pack('J', $deviceToken) . pack('n', strlen($payload)) . $payload;

#$msg = chr(0).chr(0).chr(32).bin2hex(base64_decode($deviceToken)).chr(0).chr(strlen($payload)).$payload;
$msg = chr(0).chr(0).chr(32).bin2hex(base64_decode($deviceToken)).chr(0).chr(strlen($payload)).$payload;

// Send it to the server
# $result = fwrite($fp, $msg, strlen($msg));
$result = fwrite($fp, $msg);

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);

使用 base64 编码的设备令牌

$deviceToken = '6hPQc2HvdA20XGlQkznJxlappCrzm3kJccljjOsgF2k=\n'; #base64 encoded

我也尝试过没有base64编码的设备令牌,在这种情况下我的令牌是

$deviceToken = '\xEA\u0013\xD0sa\xEFt\r\xB4\\iP\x939\xC9\xC6V\xA9\xA4*\xF3\x9By\tq\xC9c\x8C\xEB \u0017i';

要检查证书验证,以下命令工作正常。

openssl s_client -connect gateway.push.apple.com:2195 -cert apns-dev.pem -key key.pem -CAfile entrust_2048_ca.cer

执行上述 php 代码时,我没有收到任何错误。但是设备从不联系我们的 MDM 服务器来拉取配置更改。

当我的TestMDM 安装的配置文件,有 我的 MDM 安装配置文件丢失

“签名证书”部分是否会导致问题,设备未收到通知?,一个截图是 TestMDM 供应商,第二个是我的即 MobiLock

4

1 回答 1

2

我自己在 Windows 上使用 PHP 推送通知。我注意到的一件事可能是错误的,那就是您将主题包含在推送有效负载中。这不是必需的。

这是我编写的一些 PHP 代码,用于使用我在 TestMDM 中注册的设备进行测试。我不是 PHP 开发人员,但使用此(http://codular.com/sending-ios-push-notifications-with-php)作为基线并从我的 TestMDM 数据库中获取 pushMagic 和 deviceToken 字符串,我得到了成功发送推送。

因为我在 Windows 上,所以我也使用 PFX 证书进行推送。

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

运行脚本后,我可以在设备的日志中看到它,这表明它正在运行:

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.

希望这会有所帮助。

于 2017-07-11T09:26:25.760 回答