1

I used below this code

import {Push} from 'ionic-native';

initializeApp() {

this.platform.ready().then(() => {

  StatusBar.styleDefault();

  var push = Push.init({

    android: {

      senderID: "484777065207"

    },

    ios: {

      alert: "true",

      badge: true,

      sound: 'false'

    },

    windows: {}

  });

  push.on('registration', (data) => {

    console.log(data.registrationId);

    alert(data.registrationId.toString());

  });

  push.on('notification', (data) => {

    console.log(data);

    alert("Hi, Am a push notification");

  });

  push.on('error', (e) => {

    console.log(e.message);

  });

});

}

Result Mobile But when I tried this code I got out put in mobile , but I got only the alert display but I didn't get notifications display.. I have attached my mobile screen out put..

So can you plzz send me ionic 2 push notifications code...

1 Answer 1

1

I wrote an article to explain push notifications for both iOS and Android. See if that's useful Ionic 2 Push Notifications

Right now, I couldn't find any documentation on Ionic 2 website for Push notification. But using this link Ionic 2 Push and Phonegap plugin push, I am able to get basic notifications at least on android.

I have following code in my MainApp constructor

 constructor(platform:Platform, private app:IonicApp) {
        platform.ready().then(() => {
            StatusBar.styleDefault();
        });

        var push = Push.init({
            android: {
                senderID: "YOUR_SENDER_ID"
            },
            ios: {
                alert: "true",
                badge: true,
                sound: 'false'
            },
            windows: {}
        });

        push.on('registration', (data) => {
            console.log("registraiton id " + data.registrationId);
        });

        push.on('notification', (data) => {
            console.log(data.message);
            console.log(data.title);
            console.log(data.count);
            console.log(data.sound);
            console.log(data.image);
            console.log(data.additionalData);
        });

        push.on('error', (e) => {
            console.log(e.message);
        });

}

And on server side, I am using following code for push notification

var express = require('express');
var gcm = require('node-gcm');
var app = express();
var gcmApiKey = 'YOUR_GCM_API_KEY'; // GCM API KEY OF YOUR GOOGLE CONSOLE PROJECT

var server = app.listen(3000, function () {
    console.log('server is just fine!');
});

app.get('/', function (req, res) {
    res.send("This is basic route");
});

app.get('/push', function (req, res) {
    var device_tokens = []; //create array for storing device tokens

    var retry_times = 4; //the number of times to retry sending the message if it fails
    var sender = new gcm.Sender(gcmApiKey); //create a new sender
    var message = new gcm.Message(); //create a new message
    message.addData('title', 'PushTitle');
    message.addData('message', "Push message");
    message.addData('sound', 'default');
    message.collapseKey = 'Testing Push'; //grouping messages
    message.delayWhileIdle = true; //delay sending while receiving device is offline
    message.timeToLive = 3; //number of seconds to keep the message on
    //server if the device is offline

    //Take the registration id(lengthy string) that you logged
    //in your ionic v2 app and update device_tokens[0] with it for testing.
    //Later save device tokens to db and
    //get back all tokens and push to multiple devices
    device_tokens[0] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    sender.send(message, device_tokens[0], retry_times, function (result) {
        console.log('push sent to: ' + device_tokens);
        res.status(200).send('Pushed notification ' + device_tokens);
    }, function (err) {
        res.status(500).send('failed to push notification ');
    });
});

I started a thread Ionic 2 Push Thread on Ionic website for Push notification document, if you want, you can follow that thread.

Steps to run the server. On OS X, you might need to run following commands with sudo.

  • Download Nodejs from NodeJs. It will install node and npm in your system.
  • npm install express-generator -g
  • express MySampleApp
  • cd MySampleApp
  • npm install --save node-gcm
  • npm install
  • Change contents of app.js with above server code and then run server using following command
  • node app.js
Sign up to request clarification or add additional context in comments.

3 Comments

i Have one more doubt in this code, you mentioned sever side code but i don't know where to use that sever side code. plzz tell me where to use that code
@Ankush you have not used ionic io init command does that mean our app gets notified without that command ( i really not able to follow your answer)
@MohanGopi Please follow medium.com/@ankushaggarwal/… It has fully working client and server code on github along with detailed instructions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.