php - 使用 php 和 ionic alpha 推送通知发送推送通知

标签 php android ionic-framework

我已按照所有步骤在 here 的 ionic 框架中构建简单的应用程序.而且我在服务器端使用php代码来调用ionic 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);
    ?>

最佳答案

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

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 fragment 的 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, 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),
    'Authorization: Basic '.base64_encode($yourApiSecret)
    )
);

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

结果的输出也可能会告诉您很多关于您要发送的推送通知的状态。

关于php - 使用 php 和 ionic alpha 推送通知发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659370/

相关文章:

android - 如何在代码中动态更改 mipmap 文件夹中的图像而不使用 "direct description"?

java - 第一个使用 Apache Thrift 的程序——我应该在哪里定义接口(interface)?在客户端或服务器代码中

javascript - 简单的 jQuery 进度条

PHP并发问题,多个同时请求;互斥体?

Windows 10 上的 Android Studio 启动错误 'PluginManagerCore.getEnabledPluginRawList()'

java - 在Android(Java)中将opencv矩阵转换为字符串以存储在数据库中的最佳方法

html - 在选项卡导航中垂直居中 ul

jquery - 添加 jquery 会破坏 ionic

javascript - 背景图片占据整个屏幕

php - 使用 PHP 反转颜色并保持透明度