为什么我无法通过ionic framework在Android上接收推送通知
我编写了这个小测试应用程序来测试新的爱奥尼亚推送,在后端我使用的是安卓谷歌云消息服务。我从我前端的android GCM中获得了成功为什么我无法通过ionic framework在Android上接收推送通知,android,push-notification,ionic-framework,google-cloud-messaging,Android,Push Notification,Ionic Framework,Google Cloud Messaging,我编写了这个小测试应用程序来测试新的爱奥尼亚推送,在后端我使用的是安卓谷歌云消息服务。我从我前端的android GCM中获得了成功 data: Object canonical_ids: 0 failure: 0 multicast_id: 8830892721591033000 results: Array[1] success: 1 __proto__: Object 这是前端代码 app.run(function($ionicPlatform, $ionicPush,$http) {
data: Object
canonical_ids: 0
failure: 0
multicast_id: 8830892721591033000
results: Array[1]
success: 1
__proto__: Object
这是前端代码
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);
});
});
在我的后台
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);
}
});
但我仍然无法收到任何推送通知。我不明白为什么
还有人发现什么吗。我在后台使用
编辑问题可能出现在服务器或前端的任何位置。
我已经用计算机缩小了问题的范围。现在使用它,因为没有有效负载,我可以在
onNotification
listener中看到控制台日志。这意味着我的听众是正确的。。。部分原因是我能够通过仅使用数据字段将数据从服务器发送到前端。
现在的问题可能是Ionic.Push无法接收通知,或者可能是node gcm无法正确发送插件
看看你的呼吸,我和这些恶棍插件之间的最终决战。你能确认向服务器发送令牌的http请求已执行吗?如果我没有弄错的话,android代币会通过您的onRegister回调中的gcm以推送通知的形式发送到手机,如下所示:
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);
});
});
好了,伙计们,在我最后一次摊牌后,我是赢家。我终于明白了。我没有输入
app\u id
字段。这完全是与爱奥尼亚有关的事情,所以对于安卓用户来说就不是这样了。这是个小问题。我之前看过源代码了吗。我现在应该已经知道了。但无论如何,要解决四天的问题,它终于起作用了。
如果在评论中有任何东西不起作用,我会帮你的
流程是这样的
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)
你安装了推送通知插件吗?是的,通过
ionic plugin add phonegap plugin push
也安装了ionic config set dev_push false
ionic push--google api key AIzaSyB***************************JOVMw
ionic config set gcm_key 14****55078
它被验证了,因此我也粘贴了响应。我马上去查一下登记的事。几分钟后见,果然不行。它曾经奏效过。该死,我本该保存那个文件的。支持我的回答。您能在服务器端代码中记录req.body并确认您现在正在接收令牌吗?虽然gcm将令牌发送到您的设备,并显示在console.log中,但我认为您没有在服务器端代码中接收到令牌…我在另一端接收到令牌,执行了sender.send函数,结果成功。(这是我发布的第一个输出,我与服务器进行了验证)。