简体   繁体   中英

GCM Android notification received on device but not displaying

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"
}

I was having the same issue, using the sample provided at the node-gcm page. After struggling for quite some time i came across this blogpost: http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/ and modified my code according to the example provided in the blog post.

i guess there is an issue with "notification" object on node-gcm code, using message.addData seems to make it work,

in short replacing the message creation and sending logic as below worked for my application :

                // 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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