simple Notification - Android Android OS

Android examples for Android OS:Notification

Description

simple Notification

Demo Code


import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import java.io.IOException;

public class Main{
    private static final int NOTIFICATION_ID = 1;
    public static void simpleNotification(Context ctx) {
        Intent viewIntent = new Intent(ctx, MainActivity.class);
        PendingIntent viewPendingIntent = PendingIntent.getActivity(ctx, 0,
                viewIntent, 0);//w w w  .j  a va2 s. co  m

        NotificationCompat.Builder notificationBuilder = newNotification(
                ctx, "Simples", "Notifica??o simples").setContentIntent(
                viewPendingIntent);

        dispatchNotification(ctx, notificationBuilder.build(),
                NOTIFICATION_ID);
    }
    private static NotificationCompat.Builder newNotification(Context ctx,
            String title, String text) {

        return new NotificationCompat.Builder(ctx)
                .setSmallIcon(R.drawable.ic_like).setContentTitle(title)
                .setContentText(text).setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL);
    }
    private static void dispatchNotification(Context ctx,
            Notification notification, int id) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat
                .from(ctx);
        notificationManager.notify(id, notification);
    }
}

Related Tutorials