【问题标题】:GCM Android notification received on device but not displaying设备上收到 GCM Android 通知但未显示
【发布时间】: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


【解决方案1】:

您必须在 notification 块中添加图标字段:

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

【讨论】:

    【解决方案2】:

    使用 node-gcm 页面上提供的示例,我遇到了同样的问题。在挣扎了很长一段时间后,我发现了这篇博文:http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/ 并根据博文中提供的示例修改了我的代码。

    我猜 node-gcm 代码上的“通知”对象存在问题,使用 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);
                        });
                    }
    

    【讨论】:

      【解决方案3】:

      将您的服务器 IP 添加到白名单并重试: console.developers.google.com > APIs & auth > credential > 选择你的服务器密钥 > 并添加你的服务器 IP

      【讨论】:

      • 将我的服务器的IP地址添加到白名单,但不幸的是没有运气!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      相关资源
      最近更新 更多