简体   繁体   English

从php向Android应用发送推送通知

[英]Sending push notification from php to android app

working in android app development using Ionic framework as frontend and php as a backend. 使用Ionic框架作为前端和php作为后端在android应用程序开发中工作。 can someone suggest your points how can we achieve this. 有人可以建议您的观点如何实现这一目标。 am ready with sample app (followed the below steps) 1. created sample app using below comments 准备好使用示例应用程序(遵循以下步骤)1.使用以下注释创建示例应用程序

ionic start devdactic-android-push
cd devdactic-android-push
ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push
ionic io init

2. updated app.js as below $ionicPlatform.ready(function() { 2.更新了app.js,如下所示:$ ionicPlatform.ready(function(){

    $ionicPlatform.ready(function() {
         var push = new Ionic.Push({
          "debug": true
         });

         push.register(function(token) {
           console.log("Device token:",token.token);
        });
     });
  });
  1. run the below commend to get the device token 运行以下命令以获取设备令牌

    Ionic serve 离子发球

  2. I could get the device token in cosole 我可以在cosole中获取设备令牌

  3. I need to get device token of real android phone so prepared the apk file 我需要获取真正的android手机的设备令牌,因此准备了apk文件

    ionic platform add android ionic build android ionic平台添加android ionic构建android

  4. Install the generated apk in phone 在手机中安装生成的APK
  5. got the device token of real device from console( used the chrome inspector to check android app console) 从控制台获取真实设备的设备令牌(使用chrome inspector检查android应用程序控制台)

  6. created one project in google cloud platform so i have both project number(GCM console) and device token 在Google云端平台中创建了一个项目,因此我同时拥有了项目编号(GCM控制台)和设备令牌

project id - 257581368411 Device Token - DEV-e51b469d-9024-4d88-a0a9-1147f45b13f4 项目ID-257581368411设备令牌-DEV-e51b469d-9024-4d88-a0a9-1147f45b13f4

how to send notification from PHP script using above values? 如何使用上述值从PHP脚本发送通知?

and below is my system information: 以下是我的系统信息:

Cordova CLI: 6.1.1
Ionic Version: 1.2.4
Ionic CLI Version: 1.7.14
Ionic App Lib Version: 0.7.0
OS: Windows 7 SP1
Node Version: v5.0.0

You can use the script below to send push notifications 您可以使用下面的脚本发送推送通知

Source: https://gist.github.com/prime31/5675017 资料来源: https : //gist.github.com/prime31/5675017

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );

$registrationIds = array( $_GET['id'] );

// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

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

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