使用php和ionic alpha推送通知

4
我已经按照这里的步骤构建了Ionic框架下的简单应用程序,并且我使用php代码作为服务器端调用Ionic推送通知API。 我使用了以下代码,但是我的应用程序中没有收到通知,请提供解决方案。
    <?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);
    ?>
2个回答

4

我从未接触过Ionic,但根据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, 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);

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

谢谢您的回复,我已经尝试过了,但屏幕上显示的响应是 false。 - Santosh Shinde
你尝试过使用错误的API密钥吗?当我运行上面的代码时,响应是{"result":"error","message":"无法验证身份"},你应该会得到类似的结果。 - Ugur
是的,我尝试了多种选项,但是我没有得到解决方案。 - Santosh Shinde
我认为你需要分享 $result 和 curl_error($ch) 的转储。 - Ugur

2
我创建了一个完整的库,允许您使用Ionic Cloud API发送推送通知(普通和定时),获取分页列表发送推送通知、获取通知消息、获取已注册设备的信息、通过令牌删除已注册设备等等。
该库需要PHP 5.1+和cURL。

安装:

composer require tomloprod/ionic-push-php

发送通知:

use Tomloprod\IonicApi\Push;

$ionicPushApi = new Push($ionicProfile, $ionicAPIToken);

// Configuration of the notification
$notificationConfig = [
    'title' => 'Your notification title',
    'message' => 'Your notification message. Bla, bla, bla, bla.'
];

// [OPTIONAL] You can also pass custom data to the notification. Default => []
$payload = [ 
    'myCustomField' => 'This is the content of my customField',
    'anotherCustomField' => 'More custom content'
];

// [OPTIONAL] And define, if you need it, a silent notification. Default => false
$silent = true;

// [OPTIONAL] Or/and even a scheduled notification for an indicated datetime. Default => ''
$scheduled = '2016-12-10 10:30:10';

// [OPTIONAL] Filename of audio file to play when a notification is received. Setting this to default will use the default device notification sound. Default => 'default'
$sound = 'default';

// Configure notification:
$ionicPushApi->notifications->setConfig($notificationConfig, $payload, $silent, $scheduled, $sound);

// Send notification to all registered devices:
$ionicPushApi->notifications->sendNotificationToAll();

// or send notification to some devices:
$ionicPushApi->notifications->sendNotification([$desiredToken1, $desiredToken2, $desiredToken3]);

您可以在这里了解有关该库的更多信息:https://github.com/tomloprod/ionic-push-php


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接