繁体   English   中英

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

[英]GCM Android notification received on device but not displaying

尽管在应用程序中注册了通知,但我的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});
                });
              }
            }
          }
    });

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM