为什么我无法通过离子框架在 Android 上收到推送通知
Why am I unable to receive push Notification on Android by ionic framework
我编写了这个小型测试应用程序来测试新的离子推送,我在后端使用 Android Google 云消息服务。我在前端
中的 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 无法正确发送插件
看看你的呼吸,我和这些坏蛋插件之间的最终对决。
你能确认向服务器发送令牌的http请求被执行了吗?如果我没记错的话,android 令牌是在你的 onRegister 回调中通过 gcm 发送到 phone 的推送通知中收到的:
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_id
字段。它与 ionic 完全相关,因此对 android 人而言并非如此。这是一个小问题。如果我之前看过源代码。我早就知道了。但是无论如何要解决四天,终于可以解决了。
如果评论中有任何问题 post,我会帮助您。
所以流程是这样的。
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)
我编写了这个小型测试应用程序来测试新的离子推送,我在后端使用 Android Google 云消息服务。我在前端
中的 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 无法正确发送插件
看看你的呼吸,我和这些坏蛋插件之间的最终对决。
你能确认向服务器发送令牌的http请求被执行了吗?如果我没记错的话,android 令牌是在你的 onRegister 回调中通过 gcm 发送到 phone 的推送通知中收到的:
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_id
字段。它与 ionic 完全相关,因此对 android 人而言并非如此。这是一个小问题。如果我之前看过源代码。我早就知道了。但是无论如何要解决四天,终于可以解决了。
如果评论中有任何问题 post,我会帮助您。
所以流程是这样的。
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)