Android Intent Set pushNotification(Context context, int notId, PendingIntent pIntent, int iconRID, String title, String summary, boolean autoCancel, boolean vibrate, boolean light, boolean sound)

Here you can find the source of pushNotification(Context context, int notId, PendingIntent pIntent, int iconRID, String title, String summary, boolean autoCancel, boolean vibrate, boolean light, boolean sound)

Description

Build and push notification to notification center

Parameter

Parameter Description
context a parameter
notId notification Id (to cancel etc..)
pIntent PendingIntent to send when the notification is clicked
iconRID Resource Id for small Icon (-1 if not needed)
title Title of the notification
summary summary of the notification
autoCancel automatically canceled when the user clicks it in the panel
vibrate set the VIBRATE flag
light set the LIGHT flag
sound set the LIGHT flag

Declaration

public static void pushNotification(Context context, int notId,
        PendingIntent pIntent, int iconRID, String title,
        String summary, boolean autoCancel, boolean vibrate,
        boolean light, boolean sound) 

Method Source Code

//package com.java2s;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

import android.content.Context;

import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.NotificationCompat;

public class Main {
    public static final long WAKE_SCREEN_LENGTH = 3000;

    /**/*from www  . ja va2  s .  c o  m*/
     * Build and push notification to notification center
     * 
     * @param context
     * @param notId
     *            notification Id (to cancel etc..)
     * @param pIntent
     *            PendingIntent to send when the notification is clicked
     * @param iconRID
     *            Resource Id for small Icon (-1 if not needed)
     * @param title
     *            Title of the notification
     * @param summary
     *            summary of the notification
     * @param autoCancel
     *            automatically canceled when the user clicks it in the panel
     * @param vibrate
     *            set the VIBRATE flag
     * @param light
     *            set the LIGHT flag
     * @param sound
     *            set the LIGHT flag
     */
    public static void pushNotification(Context context, int notId,
            PendingIntent pIntent, int iconRID, String title,
            String summary, boolean autoCancel, boolean vibrate,
            boolean light, boolean sound) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context).setContentTitle(title).setContentText(summary);

        if (iconRID != -1)
            mBuilder.setSmallIcon(iconRID);
        mBuilder.setContentIntent(pIntent);
        mBuilder.setAutoCancel(autoCancel);
        Notification notif = mBuilder.build();
        if (vibrate)
            notif.defaults |= Notification.DEFAULT_VIBRATE;
        if (light)
            notif.defaults |= Notification.DEFAULT_LIGHTS;
        if (sound)
            notif.defaults |= Notification.DEFAULT_SOUND;

        //Log.i("TAG", "Notifying");
        mNotificationManager.notify(notId, notif);

        //Log.i("TAG", "Waking up the phone");
        // wakelock has to be called in order to show the notification
        // while the phone is locked
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire(WAKE_SCREEN_LENGTH);
    }
}

Related

  1. initIntentFilterFromXml(IntentFilter inf, XmlPullParser xpp)
  2. updateIntentExplicitness(Context context, Intent implicitIntent)
  3. getAppStartIntent(Context context)
  4. dumpBundleKeys(Intent intent)
  5. setBlankContentIntent(Context context, NotificationCompat.Builder builder)