Ionic 2 Push notification error

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!