【发布时间】:2015-09-11 10:00:37
【问题描述】:
我的 Ionic Android 应用程序的 GCM Cloud 消息通知没有出现在我设备的主屏幕中,尽管通知已在应用程序本身中注册。
我正在使用 npm 模块 node-gcm 发送推送通知。
var gcm = require('node-gcm');
var message = new gcm.Message({
priority: 'high',
contentAvailable: true,
delayWhileIdle: true,
timeToLive: 10,
dryRun: false,
data: {
key1: 'message1',
key2: 'message2'
},
notification: {
title: "Hello, World",
body: "This is a notification that will be displayed ASAP."
}
});
var regIds = ['*device id*'];
var sender = new gcm.Sender('*api key*');
sender.send(message, { registrationIds: regIds }, function (err, result) {
if(err) console.error(err);
else console.log(result);
});
当我向我的设备 ID 发送推送通知时,我得到了成功的响应:
{ multicast_id: 8406385547051869000,
success: 1,
failure: 0,
canonical_ids: 0,
results: [ { message_id: '0:1441962697347777%b67ee170f9fd7ecd' } ] }
然后我在我的 Android Studio 控制台中收到以下消息:
V/GCMBroadcastReceiver﹕ onReceive: com.google.android.c2dm.intent.RECEIVE
V/GCMBroadcastReceiver﹕ GCM IntentService class: com.plugin.gcm.GCMIntentService
V/GCMBaseIntentService﹕ Acquiring wakelock
V/GCMBaseIntentService﹕ Intent service name: GCMIntentService-GCMIntentService-5
D/GCMIntentService﹕ onMessage - context: android.app.Application@2dc6dbff
V/GCMBaseIntentService﹕ Releasing wakelock
在 Google Play 开发者控制台 GCM 调试器中,我的通知似乎也已得到确认。
0: 1441899623073525% b67ee170f9fd7ecd Confirmed
除此之外,当收到通知时,我在 Android Studio 控制台中没有收到任何错误消息。
Ionic 应用程序本身会在我发送通知后注册通知。但是,当我离开应用程序时。主屏幕上不显示任何通知。
$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
alert(notification);
if (ionic.Platform.isAndroid() && notification.event == "registered") {
window.localStorage['token'] = notification.regid;
var params = {
deviceType: 'android',
tokenId: notification.regid
};
NotificationService.registerDevice(params);
}
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge);
}
//notifications payload
if (notification.foreground == '0') {
if (notification.view) {
$timeout(function () {
$state.go('app.notifications');
});
} else {
if (notification.id) {
NotificationService.markNotificationAsRead(notification.id).success(function () {
$rootScope.$emit('notifications-read');
});
}
if (notification.chat) {
$timeout(function () {
$state.go('app.message', {id: notification.chat});
});
} else if (notification.post) {
$timeout(function () {
$state.go('app.singlePost', {id: notification.post});
});
} else if (notification.group) {
$timeout(function () {
$state.go('app.group', {id: notification.group});
});
}
}
}
});
【问题讨论】:
-
你知道没有自动通知吗?你必须为此编写自己的代码(我在你的问题中没有看到 onMessage 的代码)......而且在较新的 api 上也有可能禁用来自这个特定应用程序的通知
-
不小心提前发布了问题,不得不重新编辑。我现在已经在上面发布了我的代码
-
@Selvin 你错了。有自动通知,但功能有限
标签: javascript android cordova ionic-framework google-cloud-messaging