ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Not getting the notification title and body in android notification bar #200

Closed anshad closed 8 years ago

anshad commented 8 years ago

When I am running my code, I am getting notification on my android device (Lolipop) notification bar with title GCM Message and the icon. But I am not able to see the title and body provided in the push message.

var gcm = require('node-gcm');

var message = new gcm.Message();

message.addNotification({
     title: 'Alert!!!',
     body: 'Abnormal data access',
     icon: 'ic_launcher'
});

// Set up the sender with you API key
var sender = new gcm.Sender('MY_API_KEY');

// Add the registration tokens of the devices you want to send to
var registrationTokens = [];
registrationTokens.push('MY_DEVICE_TOKEN');

sender.send(message, { registrationTokens: registrationTokens }, function (err, response) {
   if(err) console.error(err);
   else     console.log(response);
});
eladnava commented 8 years ago

@anshad Are you by any chance using Cordova/Phonegap or is your Android app native?

anshad commented 8 years ago

I am using native app and the code is copied from https://github.com/googlesamples/google-services/tree/master/android/gcm

eladnava commented 8 years ago

Try adding data to the message, if I recall, GCM is bugged when sending only a notification object without a data object:

message.addData('test', 'test');

anshad commented 8 years ago

Tried, still not working. Found something strange in the sample code I am using,

private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("GCM Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

Looks like here the Android code manually sets something different. Help me to update this.

anshad commented 8 years ago

Thanks, the issue was with my Android parsing function. It was expecting message in the notification context.