简体   繁体   English

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

[英]Send push notification using php and ionic alpha push notification

I have follow the all steps to built simple app in ionic framework from here .And I am using php code for server side to call the ionic API for push notification . 我遵循的所有步骤,以在离子框架构建简单的应用程序从这里 。而我使用的PHP代码,服务器端调用离子的推送通知API I have Used the following code but i am not getting notification in my app , Please suggest the solution. 我使用了以下代码,但我没有在我的应用程序中收到通知,请建议解决方案。

    <?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);
    ?>

I've never dealt with ionic but according to the python example given in http://docs.ionic.io/v1.0/docs/push-sending-push you should add Authorization header to the request. 我从未处理过离子,但根据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)

PHP implementation of that python snippet would be like that; 那个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);

Also output of the result may tell you a lot about the status of push notification you want to send. 此外,输出结果可能会告诉您很多关于要发送的推送通知的状态。

I have created a complete library that allows you to consume the Ionic Cloud API for sending push notifications ( normal and scheduled ), get a paginated list of sending push notifications, get notifications messages, get information of registered devices, remove registered devices by token, etc. 我创建了一个完整的库,允许您使用Ionic Cloud API发送推送通知( 正常和预定 ),获取发送推送通知的分页列表,获取通知消息,获取已注册设备的信息,通过令牌删除已注册的设备,等等

This library requires PHP 5.1+ and cURL . 该库需要PHP 5.1+cURL


Installation: 安装:

composer require tomloprod/ionic-push-php

Sending a notification: 发送通知:

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]);

You can read more about this library here: https://github.com/tomloprod/ionic-push-php 您可以在这里阅读有关此库的更多信息: https//github.com/tomloprod/ionic-push-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM