Skip to content

Instantly share code, notes, and snippets.

@fedor
Last active February 19, 2020 14:04
Show Gist options
  • Save fedor/d0d3559ff75bcd7e68fb to your computer and use it in GitHub Desktop.
Save fedor/d0d3559ff75bcd7e68fb to your computer and use it in GitHub Desktop.
Phonegap Plugin Push Tutorial

Video: https://www.youtube.com/watch?v=_7K1iul-oCo

Plugin: https://github.com/phonegap/phonegap-plugin-push

node-gcm: https://www.npmjs.com/package/node-gcm

.JS code:

var bacekendserver = "fill in"; // depends on your implementation of the backendserver.
var adress = "fill in"; // depends on your implementation of the backendserver.
var user_id = "fill in";
var LOLApp = angular.module("LOLApp", ["ionic"]);
LOLApp.service("LOLSvc", ["$rootScope", "$http", LOLSvc]);
LOLApp.controller("LOLCtrl", ["$scope", "LOLSvc", LOLCtrl]);

function LOLCtrl($scope, LOLSvc) {
    $scope.regGCM = function() {
        LOLSvc.regGCM();
    }
}

function LOLSvc($rootScope, $http) {
    return {
        regGCM: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            var push = PushNotification.init({
                "android": {
                    "senderID": "fill in",
                    "iconColor": "green"
                },
                "ios": {},
                "windows": {}
            });
            push.on('registration', function(data) {
                var device = data.registrationId;
                console.log("registration event");
                console.log("user_id : " + user_id);
                if (user_id) {
                    console.log(bacekendserver + adress + user_id);
                    console.log("deviceID : " + device);
                    $http.post(bacekendserver + adress + user_id, {
                        device: device
                    }).
                    success(function(data, status, headers, config) {
                        console.log("Successfully registered");
                    }).
                    error(function(err) {
                        console.error('ERR', err);
                    });
                } else {
                    console.log("Registering Failed : user_id : " + user_id);
                }
                console.log(JSON.stringify(data));
            });

            push.on('notification', function(data) {
                console.log("notification event");
                console.log(JSON.stringify(data));
            });

            push.on('error', function(e) {
                console.log("push error");
            });
        }
    }
}

.HTML code:

<!DOCTYPE html>
<html lang="en" ng-app="LOLApp">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title> Bare Knuckle </title>
    <!--Setup Ionic-->
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="index.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="index.js"></script>
    <!-- Setup Ionic Cordova -->
    <script src="cordova.js">
    </script>
</head>

<body ng-controller="LOLCtrl">
    <ion-pane>
        <ion-header-bar class="bar bar-header bar-dark">
            <h1 class="title"> pushpush phonegap-plugin-push </h1>
        </ion-header-bar>
        <ion-content class="has-header">
            <ion-list>
                <ion-item>
                    <button class="button button-block button-balanced" ng-click="regGCM()">
                        Register Device
                    </button>
                </ion-item>
            </ion-list>
        </ion-content>
    </ion-pane>
</body>

</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment