0

I am trying to set multiple content in Android push notification by refer to Notification Builder

Here are the codes:

public void onReceive(Context context, Intent intent) {
    CharSequence eventName = intent.getStringExtra("eventName");
    CharSequence eventTime = intent.getStringExtra("eventTime");
    CharSequence eventAddr = intent.getStringExtra("eventAddr");

    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
            new Intent(), 0);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(eventName)
            .setContentText(
                    "Event venue: " + eventAddr + " Event Time: "
                            + eventTime)
            .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
            .setWhen(System.currentTimeMillis());
    Notification notification = builder.build();
    mNotificationManager.notify(
            Integer.parseInt(intent.getExtras().get("NotifyCount")
                    .toString()), notification);
}

But I am getting these error message:

11-26 17:44:44.943: I/dalvikvm(32480): Could not find method android.app.Notification$Builder.build, referenced from method nyp.edu.eneighbourhood.Alarm.onReceive
11-26 17:44:44.943: W/dalvikvm(32480): VFY: unable to resolve virtual method 296: Landroid/app/Notification$Builder;.build ()Landroid/app/Notification;
11-26 17:44:44.943: D/dalvikvm(32480): VFY: replacing opcode 0x6e at 0x0067
11-26 17:44:44.959: D/AndroidRuntime(32480): Shutting down VM
11-26 17:44:44.959: W/dalvikvm(32480): threadid=1: thread exiting with uncaught exception (group=0x40c7e1f8)
11-26 17:44:44.990: E/AndroidRuntime(32480): FATAL EXCEPTION: main
11-26 17:44:44.990: E/AndroidRuntime(32480): java.lang.NoSuchMethodError: android.app.Notification$Builder.build
11-26 17:44:44.990: E/AndroidRuntime(32480):    at nyp.edu.eneighbourhood.Alarm.onReceive(Alarm.java:38)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2133)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.app.ActivityThread.access$1500(ActivityThread.java:127)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.os.Looper.loop(Looper.java:137)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at android.app.ActivityThread.main(ActivityThread.java:4512)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at java.lang.reflect.Method.invokeNative(Native Method)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at java.lang.reflect.Method.invoke(Method.java:511)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
11-26 17:44:44.990: E/AndroidRuntime(32480):    at dalvik.system.NativeStart.main(Native Method)

Any guides?

Thanks in advance.

Edit

With these codes, my notification just shows up for a few seconds and it disappeared automatically without me clicking on it. Any ideas?

Community
  • 1
  • 1
  • What is the line 38 on `nyp.edu.eneighbourhood.Alarm.onReceive` ? – Batuhan Coşkun Nov 26 '14 at 10:00
  • @BatuhanC It's fixed now as I have to use NotificationCompat.Builder instead. But with these codes, the push notification will just disappear by itself after few seconds –  Nov 26 '14 at 10:01

3 Answers3

1
    NotificationCompat.Builder n;
    n=new NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle("Hello")
                    .setContentText("Notification")
                    .setContentIntent(pintent)
                    .setSmallIcon(R.drawable.ic_launcher);


 NotificationCompat.InboxStyle large_content=new NotificationCompat.InboxStyle();

    large_content.setBigContentTitle("Hello content"); 

n.setStyle(inBoxStyle);

NotificationManagerCompat notificationManager =
                            NotificationManagerCompat.from(getApplicationContext());
                    notificationManager.notify(1001, n.build());

the one that you are using is now deprecated method. it won't be run.

pintent --> this is object of pending intent

Aiyaz Parmar
  • 562
  • 6
  • 17
  • But with these codes, the notification just showing one line of text which got cut out. How do I add multiple lines to it? And it just disappeared automatically after few seconds –  Nov 26 '14 at 09:57
  • `NotificationCompat.InboxStyle large_content= new NotificationCompat.InboxStyle(); large_content.setBigContentTitle("Hello content"); n.setStyle(inBoxStyle);` try to use this code.. – Aiyaz Parmar Nov 26 '14 at 10:04
  • Sorry but it's kinda hard to see the code. Would you mind to post it as answer? –  Nov 26 '14 at 10:05
  • Nope, the items in inboxStyle does not show up and the notification will disappear automatically after few seconds without me clicking on it. Do you have any ideas? –  Nov 26 '14 at 10:20
  • where you creating this notification? means in which class like activity or service @IWasSoLost – Aiyaz Parmar Nov 26 '14 at 11:08
  • These codes were created in a class extends to broadcastReceiver –  Nov 26 '14 at 11:34
  • `n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;` add this flags to your code it might help you to not automatically gone. – Aiyaz Parmar Nov 26 '14 at 11:40
  • Nope, does not work. The notification will disappear after I touched any part of the drawer. Any ideas? –  Nov 26 '14 at 11:56
  • no, i don't know now where you are getting stuck. this should work. after that i have no idea . :( – Aiyaz Parmar Nov 26 '14 at 12:00
  • Is there any way to hide the small icon inside the notification but not the status bar? –  Nov 26 '14 at 15:49
1
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("New Message");
    notificationBuilder.setContentText("You've received new message.");
    notificationBuilder.setTicker("New Message Alert!");
    notificationBuilder.setSmallIcon(R.drawable.background);
    notificationBuilder.setAutoCancel(true);

    // Add Big View Specific Configuration
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String[] events = new String[5];
    events[0] = "This is First Line";
    events[1] = "This is Second Line";
    events[2] = "This is Third Line";
    events[3] = "This is Fourth Line";
    events[4] = "This is Fifth Line";

    // Setting the title for Inbox style big view
    inboxStyle.setBigContentTitle("Big Title Details : ");

    // Adding the event messages to the big view
    for (int i = 0; i < events.length; i++)
        inboxStyle.addLine(events[i]);

    // Adding the big message for the notification
    notificationBuilder.setStyle(inboxStyle);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* Notification Id */, notificationBuilder.build());
Jagadeesh
  • 239
  • 5
  • 9
  • But do you have any ideas why my notification will just disappear by itself after few seconds? As I cannot concatenate the string, if I do not do that, everything works fine –  Nov 26 '14 at 11:01
  • This will work for large text and initially you will get the notification with New Message \n You've received new Message. If you drag that message down you can see the whole content. – Jagadeesh Nov 26 '14 at 11:05
  • I guess its disappearing from the device screen, but should stay in Notification drawer if you do nothing. – Jagadeesh Nov 26 '14 at 11:19
  • Nope, it just appear in the drawer for a few seconds and dissapear after that without me clicking/selecting on it –  Nov 26 '14 at 11:21
  • Inside of the notification drawer, If you drag that message down you can see the inbox style. – Jagadeesh Nov 26 '14 at 11:59
  • I tried to drag down the message but still the inbox style is not showing :( Is it supported by android SDK 15? –  Nov 26 '14 at 12:19
  • I am dealing with something like this as well, just curious how do you handle the click event when someone clicks on the notification? – Jayce May 10 '15 at 02:45
0

I added and change a few things from the another answer and:

NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context)
                .setContentTitle("Hello")
                .setContentText("Notification")
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_launcher);
                .setStyle(new NotificationCompat.InboxStyle());
                .setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) 
                context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
notif.flags = Notification.FLAG_ONGOING_EVENT; // this flag added
notificationManager.notify(1, notif);

But with this, your notification will not cancel. Maybe you can walk from here.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
  • What does the setAutoCancel do? And how do I define the style? –  Nov 26 '14 at 10:14
  • Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. – Batuhan Coşkun Nov 26 '14 at 10:17
  • But then with these codes, the notification automatically disappeared after few seconds without me clicking on it. –  Nov 26 '14 at 10:18
  • It can be because of your notification is set from a BroadcastReceiver. – Batuhan Coşkun Nov 26 '14 at 10:21
  • Yeah, so how should I fix this? If I do like normal notification, the problem would not exist. Also, the items in InboxStyle are not showing –  Nov 26 '14 at 10:22
  • Nope it does not work. This only happens when I try to attach a large amount of info to notification. Let's say I only attach a small string and it would stay. –  Nov 26 '14 at 11:24