create Notification via NotificationCompat - Android Android OS

Android examples for Android OS:Notification Create

Description

create Notification via NotificationCompat

Demo Code

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

public class Main {
  private static NotificationCompat.Builder mBuilder;
  private static int numMessages = 0;
  private static int notifyId = 1;

  public static void createNotification(Intent resultIntent, Context current, final Class<?> destination) {
    NotificationManager mNotificationManager = (NotificationManager) current
        .getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(current).setContentTitle("Hooooooooooooot")
        .setContentText("You've received a new hoot").setSmallIcon(R.drawable.ic_owl_white).setAutoCancel(true)
        .setNumber(numMessages++);/*from  ww w . j a  v  a 2 s .  com*/

    if (numMessages > 1) {
      mBuilder.setContentText("You've received new hoots");
    }

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(current);
    stackBuilder.addParentStack(destination);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    mNotificationManager.notify(notifyId, mBuilder.build());
  }
}

Related Tutorials