create Private Notification - Android Android OS

Android examples for Android OS:Notification Create

Description

create Private Notification

Demo Code


import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.TaskStackBuilder;
import java.util.List;

public class Main{
    public static void createPrivateNotification(Context context,
            Intent intent, String contentTitle, String contentText, int id) {
        PendingIntent p = getPendingIntent(context, intent, id);

        NotificationCompat.Builder b = new NotificationCompat.Builder(
                context);/*w ww. j av a  2s . co m*/
        b.setDefaults(Notification.DEFAULT_ALL); 
        b.setSmallIcon(R.drawable.ic_notification_icon);
        b.setContentTitle(contentTitle); 
        b.setContentText(contentText); // Mensagem
        b.setContentIntent(p); 
        b.setAutoCancel(true); 

        // PUBLIC, PRIVATE, SECRET
        b.setVisibility(NotificationCompat.VISIBILITY_SECRET);

        NotificationManagerCompat nm = NotificationManagerCompat
                .from(context);
        nm.notify(id, b.build());
    }
    private static PendingIntent getPendingIntent(Context context,
            Intent intent, int id) {
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(intent.getComponent());
        stackBuilder.addNextIntent(intent);
        PendingIntent p = stackBuilder.getPendingIntent(id,
                PendingIntent.FLAG_UPDATE_CURRENT);
        return p;
    }
    public static void create(Context context, Intent intent,
            String contentTitle, String contentText, int id) {
        PendingIntent p = getPendingIntent(context, intent, id);

        NotificationCompat.Builder b = new NotificationCompat.Builder(
                context);
        b.setDefaults(Notification.DEFAULT_ALL); 
        b.setSmallIcon(R.drawable.ic_notification_icon); 
        b.setContentTitle(contentTitle);
        b.setContentText(contentText); 
        b.setContentIntent(p); 
        b.setAutoCancel(true); 

        b.setColor(Color.GREEN);

        NotificationManagerCompat nm = NotificationManagerCompat
                .from(context);
        nm.notify(id, b.build());
    }
}

Related Tutorials