getting errors while using Notification.Builder

吃可爱长大的小学妹 提交于 2019-12-13 04:45:00

问题


I am getting errors with these lines while handling notifications for different API levels. This is how i did so far:

...
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        Notification  notification;

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){

            notification = new Notification(icon, text, time);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
            notification.setLatestEventInfo(this, title, text, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } 
        else
        {
            notification = new Notification.Builder(this) // error
             .setContentTitle(title) // in
             .setContentText(tmp_task_brief)  // these
             .setSmallIcon(icon) // lines
             .setLargeIcon(null) // telling "this method call requires API level 11
             .build(); // or higher"

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
            notification.setLatestEventInfo(this, title, text, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        }
...

I don't understand how to remove these errors. Please help me.

Edit: I did applied edit as below but NotificationCompact.Builer too got deprecated method getNotification() that returns Notification object.

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB)
{
        notification = new Notification(icon, text, time);
        notification.setLatestEventInfo(this, title, text, contentIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        mNM.notify(NOTIFICATION, notification);
    } 
    else
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentIntent(contentIntent)
        .setSmallIcon(icon)
        .setTicker(text)
        .setWhen(System.currentTimeMillis())
        .setAutoCancel(true)
        .setContentTitle(title)
        .setContentText(text);

        notification = builder.getNotification();
        mNM.notify(NOTIFICATION, notification);
    }

回答1:


Use NotificationCompact.Bulider from support liberary (V4 liberary) that supports from 1.6

i think that will solves your problem.




回答2:


Finally, with the help of these guys i ended up to the solution of handling the deprecated methods:

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }


来源:https://stackoverflow.com/questions/16856391/getting-errors-while-using-notification-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!