【发布时间】:2015-11-18 22:37:19
【问题描述】:
我编写了这个小测试应用程序来测试新的 ionic push,并且在后端我使用的是 Android Google Cloud Messaging Service。我从前端的 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) {
$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);
}
});
但我仍然无法收到任何推送通知。我不明白为什么?
任何其他人发现任何东西。我在后端使用node-gcm
编辑编辑编辑
问题可能出在任何服务器或前端。
我已经使用this 准确地缩小了问题的范围。现在使用它,因为没有有效负载,我可以在onNotification 侦听器中看到控制台日志。这意味着我的听众是正确的......部分是因为我能够通过仅使用数据字段将数据从服务器发送到前端。
现在的问题可能是 Ionic.Push 无法接收通知,或者可能是 node-gcm 无法正确发送插件
屏住呼吸,等待我和这些坏蛋插件之间的终极对决。
【问题讨论】:
-
你安装了推送通知插件吗?
-
是的,通过
ionic plugin add phonegap-plugin-push也做了ionic config set dev_push falseionic push --google-api-key AIzaSyB****************JOVMwionic config set gcm_key 14*****55078
标签: android push-notification ionic-framework google-cloud-messaging