generate Notification - Android Android OS

Android examples for Android OS:Notification

Description

generate Notification

Demo Code


import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Main{
    @SuppressLint("NewApi")
    public static void generateNotification(Context context,
            String message, Intent notificationIntent) {

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);/*from  w  w  w  .  j a va 2s  . c om*/

        String title = context.getString(R.string.app_name);
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(message).setContentText(title)
                .setContentIntent(intent)
                .setSmallIcon(R.drawable.ic_launcher);

        Notification notification = builder.build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
}

Related Tutorials