从php向android应用程序发送推送通知


Sending push notification from php to android app

在安卓应用程序开发中使用Ionic框架作为前端,php作为后端。有人能提出你的观点吗?我们怎样才能做到这一点。我准备好了示例应用程序(按照以下步骤操作)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.更新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. 运行以下命令以获取设备令牌

    离子服务

  2. 我可以在cosole 中获得设备令牌

  3. 我需要获得真正的android手机的设备令牌所以准备了apk文件

    离子平台添加androidionic构建android

  4. 在phone中安装生成的apk
  5. 从控制台获取真实设备的设备令牌(使用chrome检查器检查android应用控制台)

  6. 在谷歌云平台上创建了一个项目所以我有项目编号(GCM控制台)和设备令牌

项目编号:257581368411设备令牌-DEV-e51b469d-9024-4d88-a0a9-1147f45b13f4

如何使用上述值从PHP脚本发送通知?

下面是我的系统信息:

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

您可以使用下面的脚本发送推送通知

来源: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;