show Notification via NotificationCompat - Android android.app

Android examples for android.app:NotificationManager

Description

show Notification via NotificationCompat

Demo Code

import java.util.Random;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

public class Main {

  public static void showNotification(Context context, int icon, String title, String content,
      PendingIntent pendingIntent) {//from   w  w w .  j a  v  a2 s .c  om
    if (context == null) {
      return;
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(icon);
    builder.setContentTitle(title);
    builder.setContentText(content);
    builder.setOngoing(false);
    builder.setAutoCancel(true);
    if (pendingIntent != null) {
      builder.setContentIntent(pendingIntent);
    }

    NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    Random random = new Random();
    int notifyId = random.nextInt();
    notificationManager.notify(notifyId, builder.build());
  }

}

Related Tutorials