2

firstly, I read this and use this simple code to multiple notification:

    public static void notifyMessage()
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
     int notifyID = 1;
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    int numMessages = 0;
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker")
            .setContentText("Events received")
            .setNumber(++numMessages)
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));
    Notification notification = builder.build();
    notificationManager.notify(1, notification);

It's working. if message's length is bigger than usual limit , it will show "bigbig...".
in next step, I want use user's message to show in notification. so I've changed function to :

      public static void notifyMessage(String message , boolean flashLed)
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
     int notifyID = 1;
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    int numMessages = 0;
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker")
            .setContentText(message)
            .setNumber(++numMessages)
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(message));
    if (flashLed)
    {
        builder.setLights(0xFFFF0000, 500, 500);
    }
    Notification notification = builder.build();
    notificationManager.notify(1, notification);
}

I trying to show message instead of "bigbigbig...." but always it show me short type of notification. I've also tried this:

  public static void notifyMessage(String message)
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
     NotificationCompat.InboxStyle inboxStyle =
                    new NotificationCompat.InboxStyle();
     String[] events = new String[4];
            // Sets a title for the Inbox in expanded layout
     events[0] = message.substring(0, 15);
     events[1] = message.substring(15, 35);
     events[2] = message.substring(35, 55);
     events[3] = message.substring(55, 75);
     inboxStyle.setBigContentTitle("Event tracker details:");

      // Moves events into the expanded layout
    for (int i=0; i < events.length; i++) {

       inboxStyle.addLine(events[i]);
    }
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker"")
            .setContentText("Events received")
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_MAX)
            .setStyle(inboxStyle);

    Notification notification = builder.build();
    notificationManager.notify(1, notification);
}

but application getting crash when new message arrive. How to change second or third function?

Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81

5 Answers5

0

remove content from .setContentText("")

Instead add:

.setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message));

Example:

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(GetNotificationIcon())
                        .setContentTitle(Constants.APP_NAME)
                        .setContentText("")
                        .setSound(uri)
                        .setVibrate(vibrate)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message));
Govind
  • 2,482
  • 1
  • 28
  • 40
-1

Please see this link - https://developer.android.com/guide/topics/ui/notifiers/notifications.html

    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
      inboxStyle.setBigContentTitle("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig");
   // Moves the expanded layout object into the notification object.
      builder.setStyle(inBoxStyle);
Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42
-1

Instead of setting the style directly to the builder add the text message to the style and then add the BigTextStyle to builder. Try to replace the following line

setStyle(new NotificationCompat.BigTextStyle()

with

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        bigText.bigText("Your long message");
        builder.setStyle(bigText);
Kartheek
  • 7,104
  • 3
  • 30
  • 44
-1

I have checked that Last method is working fine for me. Make sure you are using updated Support V4 library.

You can download from here.

Copy library in libs folder and do clean & build project and then run application.

Hope it will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
-1

The following code works for me.

 NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)

                .setContentTitle("title")
                .setContentText("message")
                .setLargeIcon(bitmap)
                .setSmallIcon(R.drawable.ic_notif)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true);

        builder.setContentIntent(pendingIntent);
        manager.notify(requestID, builder.build())
n1m1
  • 869
  • 1
  • 9
  • 20