加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用php和离子alpha推送通知发送推送通知

发布时间:2020-12-13 17:48:46 所属栏目:PHP教程 来源:网络整理
导读:我已经按照所有步骤在 here离子框架中构建简单的应用程序.我使用php代码服务器端调用离子 API for push notification.我使用了以下代码,但我没有在我的应用程序中收到通知,请建议解. ?php $androidAppId = "e2c77770"; $data = array( "tokens" = "APA91bF2Y
我已经按照所有步骤在 here离子框架中构建简单的应用程序.我使用php代码服务器端调用离子 API for push notification.我使用了以下代码,但我没有在我的应用程序中收到通知,请建议解.

<?php
    $androidAppId = "e2c77770";
    $data = array(
      "tokens" => "APA91bF2YePDKxE6K6vZYs2KQ27Z4mdehJg-EaZaPy10w-RHN5RUgC_P6Uie24Qu_M28j9bfZcbU6pu8Awofa8h2G5j9jABnebrVIUgKM5JcZPEJHYVW2NINirAm7VnSqGOrqm4YicAoI9Xiw5zkgTx4edqXIANLEhvqsqSCeq-_gAuzZB8wvrQ","notification" => "Hello World!"
    );
    $data_string = json_encode($data);
    $ch = curl_init('https://push.ionic.io/api/v1/push');
    curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array(
        'Content-Type: application/json','X-Ionic-Application-Id: '.$androidAppId,'Content-Length: ' . strlen($data_string))
    );

    $result = curl_exec($ch);
    ?>

解决方法

我从未处理过离子,但根据 http://docs.ionic.io/v1.0/docs/push-sending-push中给出的python示例,您应该在请求中添加Authorization标头.

private_key = YOUR_PRIVATE_API_KEY
b64 = base64.encodestring('%s:' % private_key).replace('n','')
#I didn't get why they used colon while encoding the api key??
req.add_header("Authorization","Basic %s" % b64)

那个python片段的PHP实现就是这样的;

<?php
$yourApiSecret = "YOUR API SECRET";
$androidAppId = "e2c77770";
$data = array(
  "tokens" => "APA91bF2YePDKxE6K6vZYs2KQ27Z4mdehJg-EaZaPy10w-RHN5RUgC_P6Uie24Qu_M28j9bfZcbU6pu8Awofa8h2G5j9jABnebrVIUgKM5JcZPEJHYVW2NINirAm7VnSqGOrqm4YicAoI9Xiw5zkgTx4edqXIANLEhvqsqSCeq-_gAuzZB8wvrQ","notification" => "Hello World!"
);
$data_string = json_encode($data);
$ch = curl_init('https://push.ionic.io/api/v1/push');
curl_setopt($ch,"POST");
curl_setopt($ch,$data_string);
curl_setopt($ch,true);
curl_setopt($ch,array(
    'Content-Type: application/json','Content-Length: ' . strlen($data_string),'Authorization: Basic '.base64_encode($yourApiSecret)
    )
);

$result = curl_exec($ch);
var_dump($result);

此外,输出结果可能会告诉您很多关于要发送的推送通知的状态.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读