【问题标题】:Send php array of device tokens to Urban Airship push将设备令牌的 php 数组发送到 Urban Airship 推送
【发布时间】:2013-11-01 20:50:29
【问题描述】:

我很烂。我正在尝试使用城市飞艇发送到设备 ID 的 php 数组。我正在使用找到here 的第一个示例。一切正常,"audience"=>"all"。每个注册的设备都会受到打击。我需要查询一个数据库,其中包含一堆设备 ID,然后发送到这些设备 ID。我应该将“audience”=>“all”更改为什么,这样我才能做到这一点。我什么都试过了!

这是链接中断的代码:

<?php
 define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/push/');

 $contents = array();
 $contents['badge'] = "+1";
 $contents['alert'] = "PHP script test";
 $contents['sound'] = "cat.caf";
 $notification = array();
 $notification['ios'] = $contents;
 $platform = array();
 array_push($platform, "ios");

 $push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);

 $json = json_encode($push);

 $session = curl_init(PUSHURL);
 curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
 curl_setopt($session, CURLOPT_POST, True);
 curl_setopt($session, CURLOPT_POSTFIELDS, $json);
 curl_setopt($session, CURLOPT_HEADER, False);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
 curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
 $content = curl_exec($session);
 echo $content; // just for testing what was sent

 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     echo "Got negative response from server, http code: ".
     $response['http_code'] . "\n";
 } else {

     echo "Wow, it worked!\n";
 }

 curl_close($session);
?>

【问题讨论】:

    标签: php curl notifications urbanairship.com


    【解决方案1】:

    这取决于您尝试发送到的设备操作系统。通过他们的文档:

    http://docs.urbanairship.com/reference/api/v3/push.html#atomic-selectors

    您需要将正确的设备类型设置为其对应的 ID。例如:

    机器人:

    "audience" : {
        "apid" : "b8f9b663-0a3b-cf45-587a-be880946e880"
    }
    

    ios:

    "audience" : {
        "device_token" : "C9E454F6105B0F442CABD48CB678E9A230C9A141F83CF4CC03665375EB78AD3A"
    }
    

    【讨论】:

    • 无论出于何种原因,该格式也不起作用。我终于使用了 Urban Airship 的 PHP 2 库。它需要安装很多额外的依赖项,但可以立即使用。谢谢!
    【解决方案2】:

    我从城市飞艇帮助中心找到了一个可能的解决方案......他们提出了这个建议。它对我有用。

    您可以在单个请求中发送到多个设备令牌或 APID。我建议使用我们的新 API v3 并批量处理您的请求。有几种方法可以做到这一点:

    1) 在一个有效载荷中发送到多个设备

    curl -v -X POST -u "&lt;AppKey&gt;:&lt;MasterSecret&gt;" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '{"audience" : {"OR": [{"device_token":"&lt;DeviceToken1&gt;"}, {"device_token":"&lt;DeviceToken2&gt;"}, {"device_token":"&lt;DeviceToken3&gt;"}]}, "notification" : {"alert" : "Hello iOS devices!"}, "device_types" : ["ios"]}' https://go.urbanairship.com/api/push/

    2) 将多个有效载荷放在一个批次中

    curl -v -X POST -u "&lt;AppKey&gt;:&lt;MasterSecret&gt;" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '[{"audience": {"device_token": "&lt;DeviceToken1&gt;"}, "notification": {"alert": "Hello, I was sent along with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "&lt;DeviceToken2&gt;"}, "notification": {"alert": "I was also sent with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "&lt;DeviceToken3&gt;"}, "notification": {"alert": "Me three!"}, "device_types": ["ios"]}]' https://go.urbanairship.com/api/push/

    【讨论】:

      【解决方案3】:

      切换到 Urban Airship 的 PHP 2 库,我能够发送到单个设备令牌。我还能够从数组中读取标记,并将数组值分配为目标。找到版本 2 here

      【讨论】:

        猜你喜欢
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多