Skip to content

Instantly share code, notes, and snippets.

@ashutoshdev-zz
Forked from tarto-dev/README.md
Created December 5, 2016 13:21
Show Gist options
  • Save ashutoshdev-zz/ac9666fc9673c6a8cceaa9675133039d to your computer and use it in GitHub Desktop.
Save ashutoshdev-zz/ac9666fc9673c6a8cceaa9675133039d to your computer and use it in GitHub Desktop.

Revisions

  1. Benjamin Cassinat revised this gist Sep 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Pour faire fonctionner les pushs sur Android / iOS, il faut ajouter ce plugin

    `cordova plugin add phonegap-plugin-push --variable SENDER_ID="134365056548`
    `cordova plugin add phonegap-plugin-push --variable SENDER_ID="xxxxxx`

    Le code dans app.js est à placer dans les directives appropriés, le fichier push.php permet de tester les pushs sur APNS & GCM

  2. Benjamin Cassinat created this gist Sep 2, 2016.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Pour faire fonctionner les pushs sur Android / iOS, il faut ajouter ce plugin

    `cordova plugin add phonegap-plugin-push --variable SENDER_ID="134365056548`

    Le code dans app.js est à placer dans les directives appropriés, le fichier push.php permet de tester les pushs sur APNS & GCM

    18 changes: 18 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    angular.module('starter', ['ionic', 'ionic.service.core', 'ngCordova'])

    .run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {
    var push = new Ionic.Push({
    "debug": true,
    "onNotification": function (notification) {
    // Once push is received by device
    },
    "onRegister": function (data) {
    // Once token registration (APNS / GCM) is done
    }
    });
    push.register(function (token) {
    // Registration callback
    });
    });
    })
    42 changes: 42 additions & 0 deletions push_android.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <?php
    // API access key from Google API's Console
    define('API_ACCESS_KEY', 'xxxxxxxxxx');
    $registrationIds = array(''); // Getted from Chrome console when you start the code above (app.js)
    // prep the bundle
    $msg = array
    (
    'message' => 'Notification content, Hello world',
    'title' => 'Notification title',
    'subtitle' => 'This is a subtitle. subtitle',
    'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate' => 1,
    'sound' => 1,
    'largeIcon' => '',
    'smallIcon' => ''
    );
    $fields = array
    (
    'registration_ids' => $registrationIds,
    'notification' => array(
    'title' => 'Titre test',
    'body' => 'Notif Content',
    'payload' => array('name' => 'Nick', 'lastname' => 'Fury')
    ),
    );

    $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;
    68 changes: 68 additions & 0 deletions push_ios.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <?php

    // Provide the Host Information.
    $tHost = 'gateway.sandbox.push.apple.com';
    $tPort = 2195;

    // Provide the Certificate and Key Data.
    $tCert = 'dev.pem';

    // Provide the Private Key Passphrase (alternatively you can keep this secrete
    // and enter the key manually on the terminal -> remove relevant line from code).
    // Replace XXXXX with your Passphrase
    $tPassphrase = 'xxxxxx';

    // Provide the Device Identifier (Ensure that the Identifier does not have spaces in it).
    // Replace this token with the token of the iOS device that is to receive the notification.
    $tToken = 'xxxxx';

    // The message that is to appear on the dialog.
    $empresa = "App Name";
    $tAlert = $empresa . ' Dialog end message !';
    // The Badge Number for the Application Icon (integer >=0).
    $tBadge = 1;
    // Audible Notification Option.
    $tSound = 'default';
    // The content that is returned by the LiveCode "pushNotificationReceived" message.
    $tPayload = '{}';
    // Create the message content that is to be sent to the device.
    $tBody['aps'] = array(
    'alert' => $tAlert,
    'badge' => $tBadge,
    'sound' => $tSound,
    );

    $tBody ['payload'] = $tPayload;

    // Encode the body to JSON.
    $tBody = json_encode($tBody);

    // Create the Socket Stream.
    $tContext = stream_context_create();
    stream_context_set_option($tContext, 'ssl', 'local_cert', $tCert);

    // Remove this line if you would like to enter the Private Key Passphrase manually.
    stream_context_set_option($tContext, 'ssl', 'passphrase', $tPassphrase);

    // Open the Connection to the APNS Server.
    $tSocket = stream_socket_client('ssl://' . $tHost . ':' . $tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $tContext);

    // Check if we were able to open a socket.
    if (!$tSocket) {
    exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
    }

    // Build the Binary Notification.
    $tMsg = chr(0) . chr(0) . chr(32) . pack('H*', $tToken) . pack('n', strlen($tBody)) . $tBody;

    // Send the Notification to the Server.
    $tResult = fwrite($tSocket, $tMsg, strlen($tMsg));
    if ($tResult) {
    echo 'Delivered Message to APNS' . PHP_EOL;
    }
    else {
    echo 'Could not Deliver Message to APNS' . PHP_EOL;
    }

    // Close the Connection to the Server.
    fclose($tSocket);