Notification like whatsapp or sms app android
Asked Answered
L

3

6

I want to create a Notification view just like whatsapp or sms notification.How can i achieve that.

enter image description here

What I have search till now

Crouton : Can found here

this will show a crouton at running activity and below to the action bar. if my activity not running or i am on the other activity how can this handle.

Toast: this is not looking like a toast. It show only on bottom or center .

Dialog: this can be done like this but this will blur the application. and disable the background. And i don't want this

Any solution will appreciated.

Thanks

Longitude answered 29/9, 2015 at 13:42 Comment(1)
What you need is a notification.(developer.android.com/guide/topics/ui/notifiers/…) whats app and all other apps have their own customized notification..if you are looking for a customized notification.i can help you. but I dont understand exactly what you need.Goolsby
K
0

what you are looking in the image above "heads up notificcation" (feature added after android 4.3)

link :heads up notification

set priority of notification as high so as to display it on screen head hope this helps

Knowlton answered 29/9, 2015 at 14:48 Comment(3)
hey how to support it from 4.3 as i thought it is from 5.0 is there any support lib or something please let me know i want that to implement in my appMourner
heads up are added feature from lollipop..i doubt there is any way you could do it @ pavanKnowlton
@ajinkyagaurkar that i know you mentioned after 4.3 so i askedMourner
M
12

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)

hope it helps

EDIT Nov-17

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

Mourner answered 29/9, 2015 at 13:50 Comment(4)
If you see the screen shot, I have not pull my notification tray. this is happening like 1. Message come Crouton shows then it appears on notification icon on status bar.Longitude
yes it is headsup notification. I will add code as answer. thanksLongitude
Now Notification.PRIORITY_MAX is Deprecated.Zlatoust
Use NotificationCompat.PRIORITY_HIGH or NotificationCompat.PRIORITY_MAX instead while using notificaion with NotificationCompat.BuilderZlatoust
L
6

It can be done via Headsup notification. It is feature of Andorid L so by code here is demo

Notification createNotification(boolean makeHeadsUpNotification) {
    Notification.Builder notificationBuilder = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setContentTitle("Sample Notification")
            .setContentText("This is a normal notification.");

    if(Build.VERSION.SDK_INT> Build.VERSION_CODES.KITKAT)
    {
        notificationBuilder.setCategory(Notification.CATEGORY_MESSAGE);
    }
    if (makeHeadsUpNotification) {
        Intent push = new Intent();
        push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        push.setClass(this, IndexActivity.class);

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
                push, PendingIntent.FLAG_CANCEL_CURRENT);
        notificationBuilder
                .setContentText("Heads-Up Notification on Android L or above.")
                .setFullScreenIntent(fullScreenPendingIntent, true);
    }
    return notificationBuilder.build();
}

you can call it like

 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context
           .NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, createNotification(
           true));
Longitude answered 30/9, 2015 at 6:53 Comment(2)
i have a doubt, if the application is closed will i get the notification..?Theoretics
@Vishal why not it will, It is a how the notification populate on screen from API 21 and further.Longitude
K
0

what you are looking in the image above "heads up notificcation" (feature added after android 4.3)

link :heads up notification

set priority of notification as high so as to display it on screen head hope this helps

Knowlton answered 29/9, 2015 at 14:48 Comment(3)
hey how to support it from 4.3 as i thought it is from 5.0 is there any support lib or something please let me know i want that to implement in my appMourner
heads up are added feature from lollipop..i doubt there is any way you could do it @ pavanKnowlton
@ajinkyagaurkar that i know you mentioned after 4.3 so i askedMourner

© 2022 - 2024 — McMap. All rights reserved.