user3321425
user3321425

Reputation: 201

Android Status bar notification with NotificationCompat.Builder. Correct way to implement?

I've been reading about how to correctly do a status bar notification, but most of the solutions use deprecated functions. I think NotificationCompat.Builder It's the best and newest solution to my problem. Anyone can give me an example code?

The best example I found not seems clear to a newbie like me. I don't know if I must implement the code in a function and call it or if I must paste the code in the activity area.

Notifications with NotificationCompat.Builder

Thank you in advance for your help and time.

Upvotes: 0

Views: 730

Answers (1)

user3321425
user3321425

Reputation: 201

Finally here's the method to create a simple notification:

public void createNotification() {


        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("Advice")
            .setContentText("blahblahblahblah.").setSmallIcon(R.drawable.icon).build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

      }

It throws a "Call requires API level 11" error, but by right clicking on project --> Android tools --> Clean Lint Markers solved the problem.

Upvotes: 1

Related Questions