GCM Android通知在设备上收到但未显示 [英] GCM Android notification received on device but not displaying
问题描述
我的设备的主屏幕上没有显示GCM我的Ionic Android应用程序云消息通知,即使通知已注册到应用程序本身。 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'
},
通知:{
title:Hello,World,
body:这是一条即将显示的通知。
}
});
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 (结果);
});
当我向我的设备ID发送推送通知时,我收到了成功的回复:
{multicast_id:8406385547051869000,
成功:1,
失败: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类:com.plugin.gcm.GCMIntentService
V / GCMBaseIntentService:获取wakelock
V / GCMBaseIntentService:意图服务名称:GCMIntentService-GCMIntentService-5
D / GCMIntentService:onMessage - context:android.app.Application@2dc6dbff
V / GCMBaseIntentService:释放唤醒锁
在Google Play Developer Console GCM调试器中,我的通知也会显示已确认。
0:1441899623073525%b67ee170f9fd7ecd已确认
除此之外,当收到通知时,我不会在Android Studio控制台收到任何错误消息。
一旦我发送了一个应用,应用本身就会注册通知。但是,当我退出应用程序。
$ rootScope。$ on('$ cordovaPush:notificationReceived',function(event,通知){
alert(通知);
if(ionic.Platform.isAndroid()&&& ] = notification.regid;
var params = {
deviceType:'android',
tokenId:notification.regid
};
NotificationService.registerDevice(params);
}
if(notification.badge){
$ cordovaPush.setBadgeNumber(notification.badge);
}
//通知有效负载
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});
});
}
}
}
});
您必须在通知
block:
通知:{
title:Hello,World ,
body:这是一条即将显示的通知,
图标:@ drawable / ic_launcher
}
GCM Cloud messaging notifications for my Ionic Android app are not appearing in my device's home screen, despite the notification registering in the app itself.
I'm using the npm module node-gcm to send push notifications.
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);
});
When I send a push notification to my device's ID, I get a successful response:
{ multicast_id: 8406385547051869000,
success: 1,
failure: 0,
canonical_ids: 0,
results: [ { message_id: '0:1441962697347777%b67ee170f9fd7ecd' } ] }
I then get the following message in my Android Studio console:
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
In the Google Play Developer Console GCM Debugger, my notifications also to appear to have been confirmed.
0: 1441899623073525% b67ee170f9fd7ecd Confirmed
Other than this I receive no error message in the Android Studio console when a notification has been received.
The Ionic app itself registers the notification once I've sent one. However when I'm out of the app. no notification is displayed in the home screen.
$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});
});
}
}
}
});
You must add icon field in your notification
block:
notification: {
title: "Hello, World",
body: "This is a notification that will be displayed ASAP.",
icon: "@drawable/ic_launcher"
}
这篇关于GCM Android通知在设备上收到但未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!