mahdi pishguy
mahdi pishguy

Reputation: 1034

Android notification shown only icon on status bar

In my application i wrote simple notification function, the method shows icon only on status bar and i want to show notification layout too, but not shown automatically and i have pull down android status bar down to see that,

public static void createNotification(Context context, Class activity, String title, String subject, int count_unread_message) {

    Intent intent = new Intent(context, activity);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notify = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(subject)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setNumber(count_unread_message)
            .setPriority(0)

            .setLights(Color.BLUE, 500, 500)
            .setContentIntent(pendingIntent)
            .build();

    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    NOTIFICATIONMANAGER = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    NOTIFICATIONMANAGER.notify(159753456, notify);
}

It seems this problem is for android 6 and above

Upvotes: 11

Views: 5234

Answers (4)

David Melo
David Melo

Reputation: 31

Add setDefaults(Notification.DEFAULT_ALL) on the Builder of Notification works for me.

Upvotes: 2

I believe your problem is that the notification banner is not being displayed, right? So the user must lower the status bar in order to see the notification.

For me what solved it was to set a higher priority to the notification. In your case you should set it to high or max (since it is already on 0 - default):

            .setPriority(1)//1 is high, 2 is max

Reference: NotificationCompat.Builder documentation

Upvotes: 2

Paul
Paul

Reputation: 2047

For me this was solved by calling setDefaults(Notification.DEFAULT_ALL) on the builder. Hope that helps,

Paul

Upvotes: 4

SaravInfern
SaravInfern

Reputation: 3388

Add .setLargeIcon(R.mipmap.ic_launcher) in your code

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.yourIcon);

 Notification notify = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(subject)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(icon)//can set only bitmap images convert your drawable to bitmap
            .setNumber(count_unread_message)
            .setPriority(0)

            .setLights(Color.BLUE, 500, 500)
            .setContentIntent(pendingIntent)
            .build();

https://developer.android.com/reference/android/app/Notification.html#largeIcon

Upvotes: 1

Related Questions