Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Why am I unable to receive push Notification on Android by ionic framework

I had written this small test app to test the New ionic push and in the backend I am using Android Google Cloud Messaging Service. I am getting success from the android GCM in my frontEnd

data: Object
canonical_ids: 0
failure: 0
multicast_id: 8830892721591033000
results: Array[1]
success: 1
__proto__: Object

This is the front End Code

app.run(function($ionicPlatform, $ionicPush,$http) {
  $ionicPlatform.ready(function() {
   $ionicPush.init({
  "onNotification": function(notification) {
    var payload = notification.payload;
    console.log(payload);
    console.log(notification);
  },
  "onRegister": function(data) {
    console.log(data.token);
  }
});

$ionicPush.register(function(data){
  var reg = {
    regId : data.token
   };
  $http.post('http://app.example.com/sendPushNot', reg)
  .then(function(response){
    console.log(response);
  }, function (error){
    console.log(error);
  });
});

and in my backend

var message = new gcm.Message({
    collapseKey: 'demo',
    priority: 'high',
    contentAvailable: true,
    delayWhileIdle: true,
    timeToLive: 3,
    //restrictedPackageName: "somePackageName",
    dryRun: false,
    data: {
        key1: 'message1',
        key2: 'message2'
    },
    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed ASAP."
    }
});

    console.log(message);
    var sender = new gcm.Sender('AIzaSyB9Lz**********9mHJuH5if1m5k5JOVMw'); 

    var regTokens =  [];
    regTokens.push(req.body.regId);

        sender.send(message, { registrationTokens: regTokens }, function (err, result) {
            if (err) {
                console.error(err);
            }
            else {

                 console.log(result);
                 res.status(200);
                 res.set('Content-Type', 'text/html');
                 res.send(result);
            }
        });   

but still I am unable to receive any push Notification. I am not understanding the reason why?

Any anybody else spot anything. I am using node-gcm in the backend

EDIT EDIT EDIT
The problem could be anywhere server or frontEnd.
I have exactly narrowed down the scope of the problem using this. Now using this as there is no payload I am able to see the console logs in onNotification listener. It means My listener is correct... partially as I am able to send the data from server to Frontend by using only data field. Now the problem could be that Ionic.Push is not capable of receiving the notifications or could be that node-gcm is not able to send the plugins correctly Behold your breath for the ultimate showdown between me and these badass plugins.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...