user2531590
user2531590

Reputation:

Android Notification Builder NoSuchMethodFound

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?

Upvotes: 0

Views: 666

Answers (3)

Jagadeesh
Jagadeesh

Reputation: 249

    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());

Upvotes: 1

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

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.

Upvotes: 0

Aiyaz Parmar
Aiyaz Parmar

Reputation: 562

    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

Upvotes: 1

Related Questions