问题
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.
回答1:
can you confirm that the http request to send token to server is executed? if i'm not mistaken, android tokens are received in a push notification to the phone via gcm in your onRegister callback as:
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);
//works for android
var reg = { regId: data.token };
$http.post('http://app.example.com/sendPushNot', reg)
.then(function(response){
console.log(response);
}, function (error){
console.log(error);
});
}
});
$ionicPush.register(function(data){
//works for ios
var reg = {
regId : data.token
};
$http.post('http://app.example.com/sendPushNot', reg)
.then(function(response){
console.log(response);
}, function (error){
console.log(error);
});
});
回答2:
Okay, guys here it goes, after my final showdown I was the winner. I figured out finally. I had not entered the app_id
field. It was something related completely to ionic so it wouldn't be true for android people. It was a small issue. Had I looked at the source code earlier. I would have known it by now. But anyways four days to solve and finally it's working.
If anythings is not working post in the comments I'll help you out.
So the flow goes like this.
ionic config set dev_push false
ionic push --google-api-key AIzaSyB9L****************5if1m5k5JOVMw
ionic config set gcm_key 148*****5078
ionic config set app_id (some_alphanumeric_value found on ionic.io and is availble after you have done ionic io init)
来源:https://stackoverflow.com/questions/33791798/why-am-i-unable-to-receive-push-notification-on-android-by-ionic-framework