send update Notification - Android Android OS

Android examples for Android OS:Notification

Description

send update Notification

Demo Code


//package com.java2s;
import java.util.Random;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class Main {
    public static int sendNotification(Context ctx, int smallIcon,
            String titile, String content, Class clazz) {
        int id = getId();
        updateNotification(ctx, id, smallIcon, titile, content, clazz,
                true, null);//  w  w  w  .  j  a va2  s. com
        return id;
    }

    public static int sendNotification(Context ctx, int smallIcon,
            String titile, String content, Class clazz, boolean autoCancel,
            String[] multiLines) {
        int id = getId();
        updateNotification(ctx, id, smallIcon, titile, content, clazz,
                autoCancel, multiLines);
        return id;
    }

    public static int getId() {
        return (new Random()).nextInt();
    }

    public static void updateNotification(Context ctx, int id,
            int smallIcon, String titile, String content, Class clazz) {
        updateNotification(ctx, id, smallIcon, titile, content, clazz,
                true, null);
    }

    public static void updateNotification(Context ctx, int id,
            int smallIcon, String titile, String content, Class clazz,
            boolean autoCancel, String[] multiLines) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ctx).setSmallIcon(smallIcon).setContentTitle(titile)
                .setContentText(content);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
        if (clazz != null) {
            Intent resultIntent = new Intent(ctx, clazz);
            stackBuilder.addParentStack(clazz);
            stackBuilder.addNextIntent(resultIntent);
        }
        if (multiLines != null && multiLines.length > 0) {
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            inboxStyle.setBigContentTitle(content);
            for (int i = 0; i < multiLines.length; i++) {
                inboxStyle.addLine(multiLines[i]);
            }
            mBuilder.setStyle(inboxStyle);
        }
        mBuilder.setAutoCancel(autoCancel);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(id, mBuilder.build());
    }
}

Related Tutorials