Android Notification Builder NoSuchMethodFound

时间:2014-11-26 09:48:34

标签: java android push-notification

我正在尝试通过参考Notification Builder

在Android推送通知中设置多个内容

以下是代码:

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

但是我收到以下错误消息:

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)

任何指南?

提前致谢。

修改

使用这些代码,我的通知只显示几秒钟,它会自动消失而不会点击它。有什么想法吗?

3 个答案:

答案 0 :(得分: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());

您正在使用的那个现在已被弃用。它不会被运行。

pintent - >这是未决意图的对象

答案 1 :(得分: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());

答案 2 :(得分:0)

我在另一个答案中添加并更改了一些内容:

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

但有了这个,您的通知将不会取消。也许你可以从这里走。