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

时间:2015-06-05 06:03:36

标签: php android ionic-framework ionic

我已经按照所有步骤在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);
    ?>

2 个答案:

答案 0 :(得分:4)

我从未处理过离子,但根据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);

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

答案 1 :(得分: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