简体   繁体   中英

Sending push notification from php to android app

working in android app development using Ionic framework as frontend and php as a backend. 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

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() {

    $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

  3. I need to get device token of real android phone so prepared the apk file

    ionic platform add android ionic build android

  4. Install the generated apk in phone
  5. got the device token of real device from console( used the chrome inspector to check android app console)

  6. created one project in google cloud platform so i have both project number(GCM console) and device token

project id - 257581368411 Device Token - DEV-e51b469d-9024-4d88-a0a9-1147f45b13f4

how to send notification from PHP script using above values?

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

// 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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