Example usage for android.support.v4.app NotificationManagerCompat from

List of usage examples for android.support.v4.app NotificationManagerCompat from

Introduction

In this page you can find the example usage for android.support.v4.app NotificationManagerCompat from.

Prototype

public static NotificationManagerCompat from(Context context) 

Source Link

Usage

From source file:com.morlunk.mumbleclient.service.PlumbleMessageNotification.java

/**
 * Dismisses the unread messages notification, marking all messages read.
 *///  w  ww. j a v a  2s  .c  om
public void dismiss() {
    mUnreadMessages.clear();
    final NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
    manager.cancel(NOTIFICATION_ID);
}

From source file:com.example.android.wearable.timer.TimerNotificationService.java

private void showTimerDoneNotification() {
    // Cancel the countdown notification to show the "timer done" notification.
    cancelCountdownNotification();/*from ww w  .  j  a  va 2 s . c  o m*/

    // Create an intent to restart a timer.
    Intent restartIntent = new Intent(Constants.ACTION_RESTART_ALARM, null, this,
            TimerNotificationService.class);
    PendingIntent pendingIntentRestart = PendingIntent.getService(this, 0, restartIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Create notification that timer has expired.
    NotificationManagerCompat notifyMgr = NotificationManagerCompat.from(this);
    Notification notif = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_cc_alarm)
            .setContentTitle(getString(R.string.timer_done)).setContentText(getString(R.string.timer_done))
            .setUsesChronometer(true).setWhen(System.currentTimeMillis())
            .addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_restart), pendingIntentRestart)
            .setLocalOnly(true).build();
    notifyMgr.notify(Constants.NOTIFICATION_TIMER_EXPIRED, notif);
}

From source file:com.example.android.wearable.synchronizednotifications.PhoneActivity.java

/**
 * Builds a local-only notification for the handset. This is achieved by using
 * <code>setLocalOnly(true)</code>. If <code>withDismissal</code> is set to <code>true</code>, a
 * {@link android.app.PendingIntent} will be added to handle the dismissal of notification to
 * be able to remove the mirrored notification on the wearable.
 *///from w ww .ja  v a 2s  . co m
private void buildLocalOnlyNotification(String title, String content, int notificationId,
        boolean withDismissal) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title).setContentText(content).setLocalOnly(true)
            .setSmallIcon(R.drawable.ic_launcher);

    if (withDismissal) {
        Intent dismissIntent = new Intent(Constants.ACTION_DISMISS);
        dismissIntent.putExtra(Constants.KEY_NOTIFICATION_ID, Constants.BOTH_ID);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, dismissIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setDeleteIntent(pendingIntent);
    }
    NotificationManagerCompat.from(this).notify(notificationId, builder.build());
}

From source file:com.example.android.wearable.wear.wearnotifications.handlers.BigTextIntentService.java

/**
 * Handles action Snooze in the provided background thread.
 *//*from  w  w w  .  j  a  v a2 s . co m*/
private void handleActionSnooze() {
    Log.d(TAG, "handleActionSnooze()");

    // You could use NotificationManager.getActiveNotifications() if you are targeting SDK 23
    // and above, but we are targeting devices with lower SDK API numbers, so we saved the
    // builder globally and get the notification back to recreate it later.

    NotificationCompat.Builder notificationCompatBuilder = GlobalNotificationBuilder
            .getNotificationCompatBuilderInstance();

    // Recreate builder from persistent state if app process is killed
    if (notificationCompatBuilder == null) {
        // Note: New builder set globally in the method
        notificationCompatBuilder = recreateBuilderWithBigTextStyle();
    }

    Notification notification;
    notification = notificationCompatBuilder.build();

    if (notification != null) {
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat
                .from(getApplicationContext());

        notificationManagerCompat.cancel(StandaloneMainActivity.NOTIFICATION_ID);

        try {
            Thread.sleep(SNOOZE_TIME);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        notificationManagerCompat.notify(StandaloneMainActivity.NOTIFICATION_ID, notification);
    }

}

From source file:net.olejon.mdapp.NotificationsFromSlvIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    final Context mContext = this;

    final MyTools mTools = new MyTools(mContext);

    if (mTools.getDefaultSharedPreferencesBoolean("NOTIFICATIONS_FROM_SLV_NOTIFY")
            && mTools.isDeviceConnected()) {
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);

        int projectVersionCode = mTools.getProjectVersionCode();

        String device = mTools.getDevice();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                getString(R.string.project_website_uri) + "api/1/notifications-from-slv/?first&version_code="
                        + projectVersionCode + "&device=" + device,
                new Response.Listener<JSONArray>() {
                    @SuppressLint("InlinedApi")
                    @Override/*from  ww  w .ja va2 s . c o m*/
                    public void onResponse(JSONArray response) {
                        try {
                            final JSONObject notifications = response.getJSONObject(0);

                            final String title = notifications.getString("title");
                            final String message = notifications.getString("message");

                            final String lastTitle = mTools
                                    .getSharedPreferencesString("NOTIFICATIONS_FROM_SLV_LAST_TITLE");

                            if (!title.equals(lastTitle)) {
                                if (!lastTitle.equals("")) {
                                    Intent launchIntent = new Intent(mContext,
                                            NotificationsFromSlvActivity.class);
                                    PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0,
                                            launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                                            R.drawable.ic_launcher);

                                    NotificationManagerCompat notificationManager = NotificationManagerCompat
                                            .from(mContext);
                                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                                            mContext);

                                    notificationBuilder.setWhen(mTools.getCurrentTime())
                                            .setPriority(Notification.PRIORITY_HIGH)
                                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                                            .setCategory(Notification.CATEGORY_MESSAGE).setLargeIcon(bitmap)
                                            .setSmallIcon(R.drawable.ic_local_hospital_white_24dp)
                                            .setSound(RingtoneManager
                                                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                            .setLights(Color.BLUE, 1000, 2000).setTicker(title)
                                            .setContentTitle(title).setContentText(message)
                                            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                                            .setContentIntent(launchPendingIntent)
                                            .addAction(R.drawable.ic_notifications_white_24dp,
                                                    getString(
                                                            R.string.service_notifications_from_slv_read_more),
                                                    launchPendingIntent);

                                    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
                                }

                                mTools.setSharedPreferencesString("NOTIFICATIONS_FROM_SLV_LAST_TITLE", title);
                            }
                        } catch (Exception e) {
                            Log.e("NotificationsFromSlv", Log.getStackTraceString(e));
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("NotificationsFromSlv", error.toString());
                    }
                });

        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        requestQueue.add(jsonArrayRequest);
    }
}

From source file:com.klinker.android.twitter.services.TrimDataService.java

public void notifyNewVersion(String version) {
    Intent goToStore = new Intent(this, RedirectToPlayStore.class);
    PendingIntent storePending = PendingIntent.getActivity(this, 0, goToStore, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Talon Version " + version);
    builder.setContentText("Click to update.");
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setSmallIcon(R.drawable.ic_stat_icon);
    builder.setContentIntent(storePending);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    notificationManager.notify(552, builder.build());
}

From source file:com.hodor.company.areminder.ui.MainActivity.java

private NotificationManagerCompat getNotificationManager() {
    if (this.mNotificationManager == null) {
        this.mNotificationManager = NotificationManagerCompat.from(this);
    }// w w  w .  j  a  va  2  s .  c o m
    return this.mNotificationManager;
}

From source file:com.negaheno.android.NotificationsController.java

public NotificationsController() {
    notificationManager = NotificationManagerCompat.from(ApplicationLoader.applicationContext);
}

From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java

public static void showLowPriorityNotification(Context context, AlarmInstance instance) {
    LogUtils.v("Displaying low priority notification for alarm instance: " + instance.mId);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context).setShowWhen(false)
            .setContentTitle(context.getString(R.string.alarm_alert_predismiss_title))
            .setContentText(AlarmUtils.getAlarmText(context, instance, true /* includeLabel */))
            .setColor(ContextCompat.getColor(context, R.color.default_background))
            .setSmallIcon(R.drawable.stat_notify_alarm).setAutoCancel(false).setSortKey(createSortKey(instance))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT).setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setLocalOnly(true);

    if (Utils.isNOrLater()) {
        notification.setGroup(UPCOMING_GROUP_KEY);
    }/*from   www  . j  a v a 2 s .  c  o  m*/

    // Setup up hide notification
    Intent hideIntent = AlarmStateManager.createStateChangeIntent(context, AlarmStateManager.ALARM_DELETE_TAG,
            instance, AlarmInstance.HIDE_NOTIFICATION_STATE);
    notification.setDeleteIntent(PendingIntent.getService(context, instance.hashCode(), hideIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.PREDISMISSED_STATE);
    notification.addAction(R.drawable.ic_alarm_off_24dp,
            context.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getService(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    Intent viewAlarmIntent = createViewAlarmIntent(context, instance);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.notify(instance.hashCode(), notification.build());
    updateAlarmGroupNotification(context);
}

From source file:com.android.deskclock.alarms.AlarmNotifications.java

public static void showHighPriorityNotification(Context context, AlarmInstance instance) {
    LogUtils.v("Displaying high priority notification for alarm instance: " + instance.mId);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.alarm_alert_predismiss_title))
            .setContentText(AlarmUtils.getAlarmText(context, instance, true /* includeLabel */))
            .setSmallIcon(R.drawable.stat_notify_alarm).setAutoCancel(false).setOngoing(true)
            .setGroup(Integer.toString(instance.hashCode())).setGroupSummary(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setLocalOnly(true);

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.PREDISMISSED_STATE);
    notification.addAction(R.drawable.ic_alarm_off_24dp,
            context.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getService(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    Intent viewAlarmIntent = createViewAlarmIntent(context, instance);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.notify(instance.hashCode(), notification.build());
}