Bakih
Bakih

Reputation: 286

notification bar - how to write text there

I want to know how to write notification text only on the notification bar and different text on the Notification layout?

Upvotes: 2

Views: 13100

Answers (4)

Jorgesys
Jorgesys

Reputation: 126523

The first text that appears in the notification bar is defined by the setTicker() method.


This is an example:

 private void showCustomNotification(){
        final int NOTIFICATION_ID = 1;
        String ns = Context.NOTIFICATION_SERVICE;
        final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.mipmap.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, getString(R.string.app_name), when);
        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Java (programming language)")
                        .setTicker("Java (Open to see the info).")
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setContentText("Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,[14] and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.");
       // NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }

enter image description here

Upvotes: 1

S.Thiongane
S.Thiongane

Reputation: 6905

The accepted answer contains deprecated class and methods. Look at here.

Here is an example using NotificationCompat.Builder to build Notifications :

        Intent notificationIntent = new Intent(this, MyClass.class);  
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.my_drawable_resource)
            .setContentTitle("my title")
            .setContentText("my content")
            .setContentIntent(pendingIntent);
        Notification notification = mBuilder.build();
            // default phone settings for notifications
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;

            // cancel notification after click
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
            // show scrolling text on status bar when notification arrives
        notification.tickerText = title + "\n" + content;

            // notifiy the notification using NotificationManager
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

May help newcomers !

Upvotes: 11

Pompair
Pompair

Reputation: 7321

The NotificationCompat.Builder class has method setTicker() that allows you to do that.

Upvotes: 9

N-JOY
N-JOY

Reputation: 7635

   int icon = R.drawable.notification_icon;        // icon from resources  
   CharSequence tickerText = "Hello";              // ticker-text  
   long when = System.currentTimeMillis();         // notification time  
   Context context = getApplicationContext();      // application Context  
   CharSequence contentTitle = "My notification";  // expanded message title  
   CharSequence contentText = "Hello World!";      // expanded message text  

   Intent notificationIntent = new Intent(this, MyClass.class);  
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);  

    // the next two lines initialize the Notification, using the configurations above  
    Notification notification = new Notification(icon, tickerText, when);  
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  

you should look at this link......... Click Me

Upvotes: 4

Related Questions