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.farmerbb.secondscreen.util.U.java

public static void runCommands(Context context, String[] commands) {
    if (getPrefMain(context).getBoolean("debug_mode", false)) {
        String dump = "";

        for (String command : commands) {
            if (!command.equals(""))
                dump = dump + context.getResources().getString(R.string.bullet) + " " + command + "\n";
        }//  ww w. ja va 2  s  .c o m

        Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle(context.getResources().getString(R.string.debug_mode_enabled))
                .setContentText(dump).setStyle(new NotificationCompat.BigTextStyle().bigText(dump)).build();

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(new Random().nextInt(), notification);

        // Some devices (Android TV) don't show notifications, so let's also print the commands
        // to the log just in case.
        System.out.println(dump);
    } else
        Shell.SU.run(commands);
}

From source file:org.ttrssreader.utils.Utils.java

/**
 * Shows a notification indicating that something is running. When called with finished=true it removes the
 * notification.//from   w  w  w .  j a v a 2 s  .  c  o  m
 *
 * @param context  the context
 * @param finished if the notification is to be removed
 */
private static void showRunningNotification(Context context, boolean finished, Intent intent) {
    if (context == null)
        return;

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

    // if finished remove notification and return, else display notification
    if (finished) {
        mNotMan.cancel(ID_RUNNING);
        return;
    }

    int icon = R.drawable.notification_icon;
    CharSequence title = context.getText(R.string.Utils_DownloadRunningTitle);
    CharSequence ticker = context.getText(R.string.Utils_DownloadRunningTicker);

    Notification notification = buildNotification(context, icon, ticker, title, "", true, intent);
    mNotMan.notify(ID_RUNNING, notification);
}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateNotification(Context context, String message) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    notificationIntent.putExtra("alert", message);
    message = message + " would like to add you as friend";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();// w ww.  j  ava 2  s . c om

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:com.commonsware.android.fullscreen.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, buildNormal().build());

    finish();/*from   w  w  w  . ja  v a2 s  .c o m*/
}

From source file:com.commonsware.android.jobsched.content.DemoJobService.java

@Override
public boolean onStartJob(JobParameters params) {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL).setContentTitle("You added a contact!")
            .setSmallIcon(android.R.drawable.stat_notify_more);

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());

    return (false);
}

From source file:com.commonsware.android.eventbus.greenrobot.ScheduledService.java

public void onEvent(RandomEvent event) {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    Intent ui = new Intent(this, EventDemoActivity.class);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
            .setContentTitle(getString(R.string.notif_title)).setContentText(Integer.toHexString(event.value))
            .setSmallIcon(android.R.drawable.stat_notify_more).setTicker(getString(R.string.notif_title))
            .setContentIntent(PendingIntent.getActivity(this, 0, ui, 0));

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
}

From source file:com.commonsware.android.eventbus.lbm.ScheduledService.java

@Override
protected void doWakefulWork(Intent intent) {
    Intent event = new Intent(EventLogFragment.ACTION_EVENT);
    long now = Calendar.getInstance().getTimeInMillis();
    int random = rng.nextInt();

    event.putExtra(EventLogFragment.EXTRA_RANDOM, random);
    event.putExtra(EventLogFragment.EXTRA_TIME, now);

    if (!LocalBroadcastManager.getInstance(this).sendBroadcast(event)) {
        NotificationCompat.Builder b = new NotificationCompat.Builder(this);
        Intent ui = new Intent(this, EventDemoActivity.class);

        b.setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
                .setContentTitle(getString(R.string.notif_title)).setContentText(Integer.toHexString(random))
                .setSmallIcon(android.R.drawable.stat_notify_more).setTicker(getString(R.string.notif_title))
                .setContentIntent(PendingIntent.getActivity(this, 0, ui, 0));

        NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mgr.notify(NOTIFY_ID, b.build());
    }/*w  w w .  j  a  v a 2  s.  c  om*/
}

From source file:com.commonsware.android.parcelable.marshall.ScheduledService.java

@Subscribe
public void onNoSubscriber(NoSubscriberEvent event) {
    RandomEvent randomEvent = (RandomEvent) event.originalEvent;
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    Intent ui = new Intent(this, EventDemoActivity.class);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
            .setContentTitle(getString(R.string.notif_title))
            .setContentText(Integer.toHexString(randomEvent.value))
            .setSmallIcon(android.R.drawable.stat_notify_more).setTicker(getString(R.string.notif_title))
            .setContentIntent(PendingIntent.getActivity(this, 0, ui, 0));

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
}

From source file:com.commonsware.android.eventbus.otto.ScheduledService.java

@Subscribe
public void onDeadEvent(DeadEvent braiiiiiiinz) {
    RandomEvent original = (RandomEvent) braiiiiiiinz.event;
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    Intent ui = new Intent(this, EventDemoActivity.class);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
            .setContentTitle(getString(R.string.notif_title))
            .setContentText(Integer.toHexString(original.value))
            .setSmallIcon(android.R.drawable.stat_notify_more).setTicker(getString(R.string.notif_title))
            .setContentIntent(PendingIntent.getActivity(this, 0, ui, 0));

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
}

From source file:com.busan.cw.clsp20120924.gcm.CommonUtilities.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///from w  ww .j ava2s  .  c  om
@SuppressWarnings("deprecation")
public static void generateNotification(Context context, GCMMessageData data) {
    String message = data.message;
    String url = data.url;
    int lightColor = 0xffff0000;
    long[] vibrate = new long[] { 0, 200, 300, 200 };

    NotificationManager mNotiManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent;
    if (data.url == null || "".equals(data.url))
        contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    else
        contentIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW, Uri.parse(url)),
                0);

    if (Build.VERSION.SDK_INT >= 14) {
        Notification.Builder mBuilder = new Notification.Builder(context);
        mBuilder.setVibrate(vibrate);
        mBuilder.setLights(lightColor, 500, 500);
        mBuilder.setContentTitle(data.title);
        mBuilder.setSmallIcon(com.busan.cw.clsp20120924.R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setContentText(message);
        mBuilder.setTicker(message);
        if (Build.VERSION.SDK_INT >= 16)
            mNotiManager.notify(data.action, mBuilder.build());
        else
            mNotiManager.notify(data.action, mBuilder.getNotification());
    } else {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setVibrate(vibrate);
        mBuilder.setLights(lightColor, 500, 500);
        mBuilder.setContentTitle(data.title);
        mBuilder.setSmallIcon(com.busan.cw.clsp20120924.R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setContentText(data.message);
        mBuilder.setTicker(data.message);
        mNotiManager.notify(data.action, mBuilder.getNotification());
    }

}