2

On receiving a message from GCM, I'm generating a notification

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MyActivity.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);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_action_chat)
            .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());
}

The notification is generated, the sound gets played, but it does not get shown at top of screen. The notification is displayed in the quick settings/notifications drop down menu, but at the time when I generate it, it is not displayed at the top of screen, only sound is played.

I want to display it at top of screen when I generate it. How can I do it?

user5155835
  • 4,392
  • 4
  • 53
  • 97
  • are you looking for head up notification check this one http://developer.android.com/design/patterns/notifications.html – Pavan Sep 21 '15 at 16:01
  • http://stackoverflow.com/questions/26451893/heads-up-notification-android-lollipop – Pavan Sep 21 '15 at 16:07

2 Answers2

3

it's a headsup notification works from android lollipop here you check how to show notification you can see here http://developer.android.com/guide/topics/ui/notifiers/notifications.html and for headsup you have to set notification priority as below

.setPriority(Notification.PRIORITY_HIGH)

EDIT Nov-17

Notification.PRIORITY_HIGH was deprecated in API level 26. use IMPORTANCE_HIGH instead.

Pavan
  • 5,016
  • 1
  • 26
  • 30
0

In my case, I had to do

.setSmallIcon(R.drawable.my_icon)
Dino Tw
  • 3,167
  • 4
  • 34
  • 48
  • I had already written that in the question, anyways setPriority worked for me – user5155835 Oct 02 '15 at 02:08
  • Just for people who encounter the same issue but different configuration. Probably I should have written it in the comment instead of answer. My bad. – Dino Tw Oct 02 '15 at 06:52