苹果推送通知服务进行连接,但没有通知收到 [英] Apple Push Notification Service connects, but no notification is recieved

查看:285
本文介绍了苹果推送通知服务进行连接,但没有通知收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲打我的头挂在墙上试图让这项工作在过去的一周。我只是从头再次启动,并记录我做的一切,它仍然无法正常工作。下面是我所采取的步骤:

I’ve been banging my head on the wall trying to get this work for the past week. I just started again from scratch and logged everything that I did and it still does not work. Here are the steps i’ve taken:

1)使用新的软件包ID(旧的没有推送通知)创建一个新的App ID标识

1) Create a new App ID Identifier using a new bundle id (the old one did not have push notifications)

a) made sure push notifications were added to the app id and proceeded to the last step, this resulted in an app id with push notifications in the ‘configurable’ state.

b) I then went to edit the app id and saw that i could create a new production or development certificate so i did both using the steps listed

2)创建了两个新的供应配置文件,一个用于开发,一个用于生产,两者都使用新的软件包ID

2) Created two new provisioning profiles, one for dev and one for production, both are using the new bundle id

3)导入新配置的配置文件到X code和确保这些都是在构建设置中选择的那些

3) import the new provisioning profiles into Xcode and make sure that those are the ones selected in the build settings

4)使用钥匙串和导出.CER文件,用于开发和生产创造中.P12文件

4) create the .p12 files by using the keychain and exporting the .cer files for dev and production

5)创建使用这些命令中.P12文件.pem文件:

5) create the .pem file from the .p12 files using these commands:

openssl pkcs12 -in VX-Mobile-Dev.p12 -out apns-dev.pem -nodes -clcerts

openssl pkcs12 -in VX-Mobile-Prod.p12 -out apns-prod.pem -nodes -clcerts 

6)接下来创建一个PHP脚本(即我大摇大摆地从另一个博客扯掉),我从我的其中一个虚拟机的命令行中运行。在同一文件夹的文件我把锅-prod.pem文件。这里是该文件的内容

6) Next created a php script (that i blatantly ripped off from another blog) that i run from the command line of one of my virtual machines. In the same folder as that file i put the pans-prod.pem file. Here is the contents of that file

请注意,该设备令牌我只需运行X code中的应用,在那里它已经成功连接到APNS服务器并获取该被注销到控制台的设备令牌,用于测试目的我只是复制新的令牌到该文件中各一次。

Note, for the device token i simply run the app from Xcode where it already successfully connects to the apns server and gets a device token which gets logged out into the console, for testing purposes i am just copying the new token into the file each time.

文件的内容:

// set time limit to zero in order to avoid timeout
set_time_limit(0);

// charset header for output
header('content-type: text/html; charset: utf-8');

// this is the pass phrase you defined when creating the key
$passphrase = '{passphrase is hidden to you!}';

// you can post a variable to this string or edit the message here
if (!isset($_POST['msg'])) {
$_POST['msg'] = "Notification message here!";
}

// function for fixing Turkish characters
function tr_to_utf($text) {
    $text = trim($text);
    $search = array('Ü', 'Þ', 'Ð', 'Ç', 'Ý', 'Ö', 'ü', 'þ', 'ð', 'ç', 'ý', 'ö');
    $replace = array('Ãœ', 'Åž', 'Ğž', 'Ç', 'Ä°', 'Ö', 'ü', 'ÅŸ', 'ÄŸ', 'ç', 'ı', 'ö');
    $new_text = str_replace($search, $replace, $text);
    return $new_text;
}

// tr_to_utf function needed to fix the Turkish characters
$message = tr_to_utf($_POST['msg']);

// load your device ids to an array
$deviceIds = array(
'97c8c0a77e1380d052d17352e33b1f2224f082a3436ee90310cf88c7278329e7'
);

// this is where you can customize your notification
$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';

$result = 'Start' . "\r\n";

////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-prod.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

echo count($deviceIds) . " devices will receive notifications.\r\n";

foreach ($deviceIds as $item) {
    // wait for some time
    sleep(1);

    // 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" . "\r\n");
    } else {
        echo 'Apple service is online. ' . "\r\n";
    }

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;

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

    if (!$result) {
        echo 'Undelivered message count: ' . $item . "\r\n";
    } else {
        echo("\r\nRESULT\r\n\r\n");
        var_dump($result);
        echo("\r\n\r\n");
        echo 'Delivered message count: ' . $item . "\r\n";
    }

    if ($fp) {
        fclose($fp);
        echo 'The connection has been closed by the client' . "\r\n";
    }
}

echo count($deviceIds) . " devices have received notifications.\r\n";

// set time limit back to a normal value
set_time_limit(30);

请帮忙。

推荐答案

原来我几乎没有。唯一的问题是,我应该使用沙盒终点都有了。

Turns out i was almost there. The only problem was that i should have been using the sandbox endpoint.

ssl://gateway.sandbox.push.apple.com:2195

此外,为应对RJV库马尔,一旦我得到它的工作我都用我的督促和我的开发证书尝试过了,它在两种情况下工作。这似乎可疑给我,但无论我只是很高兴,它的工作。

Also, in response to RJV Kumar, once i got it working i tried it with both my prod and my dev certificate and it worked in both cases. This seems suspicious to me but whatever i'm just glad that its working.

这篇关于苹果推送通知服务进行连接,但没有通知收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆