Android设备接收到GCM通知但未显示

3
我的Ionic Android应用程序中的GCM云消息通知没有出现在设备的主屏幕上,尽管该通知在应用程序内部进行了注册。
我使用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
不小心提前发布了问题,必须重新编辑。我现在已经发布了我的代码。 - JeromeShapiro
@Selvin,你错了。虽然它们的功能有限,但确实存在自动通知。 - Le_Enot
3个回答

2

您需要在notification块中添加图标字段:

notification: {
    title: "Hello, World",
    body: "This is a notification that will be displayed ASAP.",
    icon: "@drawable/ic_launcher"
}

1
我遇到了同样的问题,使用node-gcm页面提供的示例。经过一番挣扎,我发现了这篇博客文章: http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/,并根据博客文章中提供的示例修改了我的代码。
我想可能是node-gcm代码中的“notification”对象存在问题,使用message.addData似乎可以解决这个问题,
简而言之,将消息创建和发送逻辑替换为以下内容对我的应用程序有效:
                // For Android
                if (row.device === "Android" && row.deviceToken) {

                    console.log(row.deviceToken);

                    var sender = new gcm.Sender(serverApiKey);
                    var message = new gcm.Message();

                    message.addData('title','İş Geliştirme Platformu');
                    message.addData('message','Yeni İleti');
                    message.addData('msgcnt','1');

                    //message.collapseKey = 'demo';
                    message.delayWhileIdle = true;
                    message.timeToLive = 3;

                    var registrationIds = [];
                    registrationIds.push(row.deviceToken);
                    sender.send(message, registrationIds, 4, function (err, result) {
                        console.log(result);
                        res.send(200,result);
                    });
                }

0
请将您的服务器IP添加到白名单中,然后重试: console.developers.google.com > APIs & auth > 凭据 > 选择您的服务器密钥 > 添加您的服务器IP。

我将我的服务器IP地址添加到白名单中,但不幸的是没有成功! - JeromeShapiro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接