1

I am creating a small app for android for notification.

But it gives an Error in Notification class error (API level supported for 11 or 16). Then I tried using NotificationCompat class but it shows resources can not be resolved to a type while I import package import android.support.v4.app.NotificationCompat.Builder;

In other words, if I use Notification class then it give API level Error, and if I use NotificationCompat then it gives that resource error. How can I resolve both these errors?

 public void createNotification(View view) {
    // Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    // Context context=new Context();
    // Build notification
    // Actions are just fake
     NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
    .setContentTitle("New mail from " + "star.ankit90@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .addAction(R.drawable.ic_launcher, "Call", pIntent)
    .addAction(R.drawable.ic_launcher, "More", pIntent)
    .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}
Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
Ankit
  • 191
  • 1
  • 2
  • 8

1 Answers1

1

I've had compilation errors using your code.

This fixed code compiles successfully:

// Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContentTitle("New mail from " + "star.ankit90@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent)
    .addAction(R.drawable.ic_launcher, "Call", pIntent)
    .addAction(R.drawable.ic_launcher, "More", pIntent)
    .addAction(R.drawable.ic_launcher, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification);

Maybe it will work for you. if not a stack trace can help.

dors
  • 5,802
  • 8
  • 45
  • 71