GCM Android notification received on device but not displaying

纵然是瞬间 提交于 2020-01-24 09:27:18

问题


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

回答1:


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



回答2:


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



回答3:


add your server IP to white list and try again: console.developers.google.com > APIs & auth > credential > select your server key > and add your server IP



来源:https://stackoverflow.com/questions/32520760/gcm-android-notification-received-on-device-but-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!