Example usage for android.app NotificationManager notify

List of usage examples for android.app NotificationManager notify

Introduction

In this page you can find the example usage for android.app NotificationManager notify.

Prototype

public void notify(int id, Notification notification) 

Source Link

Document

Post a notification to be shown in the status bar.

Usage

From source file:com.firescar96.nom.GCMIntentService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static void Notify(String notificationTitle, String notificationMessage, Bundle data, int id,
        boolean annoy) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(thisService)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(notificationTitle)
            .setContentText(notificationMessage).setAutoCancel(true);
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(thisService, MainActivity.class);
    resultIntent.putExtras(data);//from w w w.  jav  a 2 s.  c o m

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(thisService);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    if (annoy) {
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);
        long[] pattern = { 50, 100, 10, 100, 10, 200 };
        mBuilder.setVibrate(pattern);
    }
    NotificationManager mNotificationManager = (NotificationManager) thisService
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(id, mBuilder.build());
}

From source file:cn.com.flashman.cordova.xpush.Baidu.java

static public void pushCallback(JSONObject r) {
    //???/*from  w ww .j a va2 s  . co m*/
    if (isActive && !webview.isFinishing()) {
        webview.sendJavascript("cordova.plugins.XPush.Baidu.pushCheck()");

        //?????
    } else {
        try {
            if (r.getString("type") == "message") {
                JSONObject info = new JSONObject(r.getString("message"));
                String title = info.getString("title");
                String desc = info.getString("description");
                NotificationManager nm = (NotificationManager) webview
                        .getSystemService(webview.NOTIFICATION_SERVICE);
                Notification notification = new Notification(icon, title, System.currentTimeMillis());
                notification.defaults = Notification.DEFAULT_ALL;
                PendingIntent pt = PendingIntent.getActivity(webview, 0,
                        new Intent(webview, webview.getClass()), 0);
                notification.setLatestEventInfo(webview, title, desc, pt);
                nm.notify(nitify_id, notification);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.andrewshu.android.reddit.common.Common.java

public static void newMailNotification(Context context, String mailNotificationStyle, int count) {
    Intent nIntent = new Intent(context, InboxActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
    Notification notification = new Notification(R.drawable.mail, Constants.HAVE_MAIL_TICKER,
            System.currentTimeMillis());
    if (Constants.PREF_MAIL_NOTIFICATION_STYLE_BIG_ENVELOPE.equals(mailNotificationStyle)) {
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.big_envelope_notification);
        notification.contentView = contentView;
    } else {/*from   w  ww. java  2  s  . c  o  m*/
        notification.setLatestEventInfo(context, Constants.HAVE_MAIL_TITLE,
                count + (count == 1 ? " unread message" : " unread messages"), contentIntent);
    }
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    notification.contentIntent = contentIntent;
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Constants.NOTIFICATION_HAVE_MAIL, notification);
}

From source file:com.adam.aslfms.util.Util.java

public static void myNotify(Context mCtx, String title, String content, int notID) {
    try {//from  ww w  . j  a  va  2  s .c  om
        Intent targetIntent = new Intent(mCtx, SettingsActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mCtx, 0, targetIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx).setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_notify).setContentText(content).setContentIntent(contentIntent);
        NotificationManager nManager = (NotificationManager) mCtx
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
            builder.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher));
        }
        nManager.notify(notID, builder.build());
    } catch (Exception e) {
        Log.d(TAG, "Phone Notification failed. " + e);
    }
}

From source file:com.manning.androidhacks.hack046.helper.NotificationHelper.java

public static void showMsgNotification(Context ctx) {
    final NotificationManager mgr;
    mgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(android.R.drawable.sym_def_app_icon).setTicker("New msg!")
            .setContentTitle("This is the msg title").setContentText("content...")
            .setContentIntent(getPendingIntent(ctx));

    builder.addAction(android.R.drawable.ic_menu_send, ctx.getString(R.string.activity_msg_button_reply),
            getReplyPendingIntent(ctx));

    builder.addAction(android.R.drawable.ic_menu_delete, ctx.getString(R.string.activity_msg_button_delete),
            getDeletePendingIntent(ctx));

    mgr.notify(R.id.activity_main_receive_msg, builder.build());
}

From source file:com.arubadesignweb.app.appoverview.util.GcmIntentService.java

public static void sendLocalNotification(Context context, String title, String message, String message_id) {
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Application.class),
            0);//from   w w  w  .  jav  a  2s.  com

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL).setSmallIcon(R.drawable.push_icon).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message);

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    CommonUtilities.markAsDisplayed(message_id);
}

From source file:com.cfc.needblood.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*  ww w  .j ava 2s. c  o m*/
private static void generateNotification(Context context, String message) {
    long when = System.currentTimeMillis();

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Change the icons to conform Android's design guildeline
    builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.bd_icon))
            .setTicker(message).setWhen(when).setAutoCancel(true)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(context.getString(R.string.notification_message));

    Notification notification = builder.build();

    notificationManager.notify(0, notification);

    // Notification sound, comment out if do not need
    Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, ringtone);
    r.play();
}

From source file:com.baasio.sample.startup.gcm.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from w w w .  ja v a2s.  c  o  m*/
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(context).setWhen(when).setSmallIcon(icon)
            .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
            .setContentIntent(intent).setTicker(message).setAutoCancel(true).getNotification();

    notificationManager.notify(0, notification);
}

From source file:com.cbtec.eliademy.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///from  w  ww .j a va2s .c om

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, Eliademy.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:net.mceoin.cominghome.NestUtils.java

/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the main Activity.
 *
 * @param transitionType The type of transition that occurred.
 */// ww  w  .  j  a  v a  2s  .  c om
public static void sendNotification(Context context, String transitionType) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean notifications = prefs.getBoolean(MainActivity.PREFS_NOTIFICATIONS, true);

    if (!notifications) {
        if (debug)
            Log.d(TAG, "notifications are turned off");
        return;
    }

    // Create an explicit content Intent that starts the main Activity
    Intent notificationIntent = new Intent(context, MainActivity.class);

    // Construct a task stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    // Adds the main Activity to the task stack as the parent
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Set the notification contents
    builder.setSmallIcon(R.drawable.home)
            .setContentTitle(context.getString(R.string.nest_transition_notification_title, transitionType))
            .setContentText(context.getString(R.string.nest_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}