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

时间:2015-09-11 10:00:37

标签: javascript android cordova ionic-framework google-cloud-messaging

我的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});
                });
              }
            }
          }
    });

3 个答案:

答案 0 :(得分:2)

您必须在strsplit(gsub('_NM', ',NM', s), ',') 区块中添加图标字段:

notification

答案 1 :(得分: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);
                    });
                }

答案 2 :(得分:0)

将您的服务器IP添加到白名单,然后重试: console.developers.google.com> API& auth>凭证>选择您的服务器密钥>并添加您的服务器IP