【问题标题】:Send push notification using php and ionic alpha push notification使用 php 和 ionic alpha 推送通知发送推送通知
【发布时间】:2015-08-19 23:33:15
【问题描述】:

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

【问题讨论】:

    标签: php android ionic-framework ionic


    【解决方案1】:

    我从未处理过 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 sn-p 的 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);
    

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

    【讨论】:

    • 感谢您的回复,我已经尝试过了,但屏幕上的回复错误...
    • 您是否尝试过使用虚假的 API 机密?当我运行上面的代码时,这是响应,{"result":"error","message":"Unable to Authenticate"} 你应该得到类似的东西。
    • 是的,我尝试了多种选择,但我没有得到解决方案。
    • 我认为你必须分享 $result 和 curl_error($ch) 的转储
    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-04
      • 2020-06-25
      • 1970-01-01
      • 2018-02-17
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      相关资源
      最近更新 更多