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.android.deskclock.timer.TimerReceiver.java

public static void cancelInUseNotification(final Context context) {
    NotificationManagerCompat.from(context).cancel(IN_USE_NOTIFICATION_ID);
}

From source file:org.kontalk.ui.MessagingNotification.java

/** Fires an authentication error notification. */
public static void authenticationError(Context context) {
    // notification will open the conversation
    Intent ni = ConversationsActivity.authenticationErrorWarning(context);
    PendingIntent pi = PendingIntent.getActivity(context, NOTIFICATION_ID_AUTH_ERROR, ni,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // build the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext())
            .setAutoCancel(true).setSmallIcon(R.drawable.ic_stat_notify)
            .setTicker(context.getString(R.string.title_auth_error))
            .setContentTitle(context.getString(R.string.title_auth_error))
            .setContentText(context.getString(R.string.notification_text_more)).setContentIntent(pi);

    // defaults (sound, vibration, lights)
    setDefaults(context, builder);/*from  w w w .  j a v  a  2  s  .c  om*/

    // fire it up!
    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.notify(NOTIFICATION_ID_AUTH_ERROR, builder.build());
}

From source file:org.chromium.chrome.browser.media.ui.NotificationMediaPlaybackControls.java

private void updateNotification() {
    if (mService == null)
        return;/*from w  w w . ja  v  a2  s.co m*/

    if (mMediaNotificationInfo == null) {
        // Notification was hidden before we could update it.
        assert mNotificationBuilder == null;
        return;
    }

    // Android doesn't badge the icons for RemoteViews automatically when
    // running the app under the Work profile.
    if (mNotificationIcon == null) {
        Drawable notificationIconDrawable = ApiCompatibilityUtils.getUserBadgedIcon(mContext,
                R.drawable.audio_playing);
        mNotificationIcon = drawableToBitmap(notificationIconDrawable);
    }

    if (mNotificationBuilder == null) {
        mNotificationBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(R.drawable.audio_playing)
                .setAutoCancel(false).setLocalOnly(true)
                .setDeleteIntent(mService.getPendingIntent(ListenerService.ACTION_STOP));
    }
    mNotificationBuilder.setOngoing(!mMediaNotificationInfo.isPaused);
    mNotificationBuilder.setContentIntent(createContentIntent());

    RemoteViews contentView = createContentView();

    contentView.setTextViewText(R.id.title, mMediaNotificationInfo.title);
    contentView.setTextViewText(R.id.status, getStatus());
    if (mNotificationIcon != null) {
        contentView.setImageViewBitmap(R.id.icon, mNotificationIcon);
    } else {
        contentView.setImageViewResource(R.id.icon, R.drawable.audio_playing);
    }

    if (mMediaNotificationInfo.isPaused) {
        contentView.setImageViewResource(R.id.playpause, R.drawable.ic_vidcontrol_play);
        contentView.setContentDescription(R.id.playpause, mPlayDescription);
        contentView.setOnClickPendingIntent(R.id.playpause,
                mService.getPendingIntent(ListenerService.ACTION_PLAY));
    } else {
        contentView.setImageViewResource(R.id.playpause, R.drawable.ic_vidcontrol_pause);
        contentView.setContentDescription(R.id.playpause, mPauseDescription);
        contentView.setOnClickPendingIntent(R.id.playpause,
                mService.getPendingIntent(ListenerService.ACTION_PAUSE));
    }

    mNotificationBuilder.setContent(contentView);
    mNotificationBuilder.setVisibility(mMediaNotificationInfo.isPrivate ? NotificationCompat.VISIBILITY_PRIVATE
            : NotificationCompat.VISIBILITY_PUBLIC);

    if (mMediaSession == null) {
        mMediaSession = new MediaSessionCompat(mContext, mContext.getString(R.string.app_name),
                new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName()), null);
        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mMediaSession.setCallback(mMediaSessionCallback);
        mMediaSession.setActive(true);
    }

    mMediaSession.setMetadata(createMetadata());

    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE);
    if (mMediaNotificationInfo.isPaused) {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    } else {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    }
    mMediaSession.setPlaybackState(playbackStateBuilder.build());

    Notification notification = mNotificationBuilder.build();

    // We keep the service as a foreground service while the media is playing. When it is not,
    // the service isn't stopped but is no longer in foreground, thus at a lower priority.
    // While the service is in foreground, the associated notification can't be swipped away.
    // Moving it back to background allows the user to remove the notification.
    if (mMediaNotificationInfo.isPaused) {
        mService.stopForeground(false /* removeNotification */);

        NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
        manager.notify(R.id.media_playback_notification, notification);
    } else {
        mService.startForeground(R.id.media_playback_notification, notification);
    }
}

From source file:org.kontalk.ui.MessagingNotification.java

public static void clearAuthenticationError(Context context) {
    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.cancel(NOTIFICATION_ID_AUTH_ERROR);
}

From source file:org.chromium.chrome.browser.media.ui.MediaNotificationManager.java

private void clearNotification() {
    if (mMediaNotificationInfo == null)
        return;//from   w  w  w. j a  v  a2s .co m

    NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
    manager.cancel(mMediaNotificationInfo.id);

    if (mMediaSession != null) {
        mMediaSession.setCallback(null);
        mMediaSession.setActive(false);
        mMediaSession.release();
        mMediaSession = null;
    }
    mContext.stopService(createIntent(mContext));
    mMediaNotificationInfo = null;
}

From source file:com.mylovemhz.simplay.MusicService.java

private void publishNotification(NotificationCompat.Builder builder) {
    if (contentIntent != null) {
        builder.setContentIntent(contentIntent);
    } else {//from  w  w w . j  ava2  s .  c  o  m
        Log.e(TAG_MUSIC_SERVICE, "Did you forget to setContentIntent()? Nothing will happen "
                + "when you touch the notification.");
    }
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    try {
        notificationManager.notify(ID_NOTIFICATION, builder.build());
    } catch (IllegalStateException e) {
        Log.e(TAG_MUSIC_SERVICE, "Did you forget to setSmallIconResource() in your onBind()?");
    }
}

From source file:com.tct.mail.utils.NotificationUtils.java

/**
 * Get all notifications for all accounts, optionally cancel them, and repost.
 * This happens when locale changes. If you only want to resend messages from one
 * account-folder pair, pass in the account and folder that should be resent.
 * All other account-folder pairs will not have their notifications resent.
 * All notifications will be resent if account or folder is null.
 *
 * @param context Current context./*from   w w w. j a  v  a  2  s  . c  om*/
 * @param cancelExisting True, if all notifications should be canceled before resending.
 *                       False, otherwise.
 * @param accountUri The {@link Uri} of the {@link Account} of the notification
 *                   upon which an action occurred.
 * @param folderUri The {@link Uri} of the {@link Folder} of the notification
 *                  upon which an action occurred.
 */
public static void resendNotifications(Context context, final boolean cancelExisting, final Uri accountUri,
        final FolderUri folderUri, final ContactPhotoFetcher photoFetcher) {
    //M upgrade: Bug: 17713589 Gmail crashes when accessing an old notification for an account that has been deleted.
    LogUtils.i(LOG_TAG, "resendNotifications cancelExisting: %b, account: %s, folder: %s", cancelExisting,
            accountUri == null ? null : LogUtils.sanitizeName(LOG_TAG, accountUri.toString()),
            folderUri == null ? null : LogUtils.sanitizeName(LOG_TAG, folderUri.toString()));

    if (cancelExisting) {
        LogUtils.d(LOG_TAG, "resendNotifications - cancelling all");
        NotificationManagerCompat nm = NotificationManagerCompat.from(context);
        nm.cancelAll();
    }
    // Re-validate the notifications.
    final NotificationMap notificationMap = getNotificationMap(context);
    final Set<NotificationKey> keys = notificationMap.keySet();
    for (NotificationKey notification : keys) {
        final Folder folder = notification.folder;
        final int notificationId = getNotificationId(notification.account.getAccountManagerAccount(), folder);

        // Only resend notifications if the notifications are from the same folder
        // and same account as the undo notification that was previously displayed.
        if (accountUri != null && !Objects.equal(accountUri, notification.account.uri) && folderUri != null
                && !Objects.equal(folderUri, folder.folderUri)) {
            LogUtils.d(LOG_TAG,
                    "resendNotifications - not resending %s / %s" + " because it doesn't match %s / %s",
                    notification.account.uri, folder.folderUri, accountUri, folderUri);
            continue;
        }

        LogUtils.d(LOG_TAG, "resendNotifications - resending %s / %s", notification.account.uri,
                folder.folderUri);

        final NotificationAction undoableAction = NotificationActionUtils.sUndoNotifications
                .get(notificationId);
        if (undoableAction == null) {
            validateNotifications(context, folder, notification.account, true, false, notification,
                    photoFetcher);
        } else {
            // Create an undo notification
            NotificationActionUtils.createUndoNotification(context, undoableAction);
        }
    }
}

From source file:com.googlecode.mindbell.accessors.ContextAccessor.java

/**
 * Cancel the ring notification (after ringing the bell).
 *//*from w  w w  . jav a2 s . co m*/
public void cancelRingNotification(ActivityPrefsAccessor activityPrefs) {
    NotificationManagerCompat.from(context).cancel(RING_NOTIFICATION_ID);
}

From source file:com.pacoapp.paco.triggering.NotificationCreator.java

private void fireNotification(Context context, NotificationHolder notificationHolder, String experimentTitle,
        String message, String experimentSpecificRingtone, Integer color, Boolean dismissible) {
    String alarmTimeString = "";
    final Long alarmTime = notificationHolder.getAlarmTime();
    if (alarmTime != null) {
        alarmTimeString = new DateTime(alarmTime).toString();
    }//from  www.j a va 2 s . c o  m
    Log.info("Creating notification for experiment: " + experimentTitle + ". source: "
            + notificationHolder.getNotificationSource() + ". alarmTime: " + alarmTimeString + ", holderId = "
            + notificationHolder.getId());

    Notification notification = createAndroidNotification(context, notificationHolder, experimentTitle, message,
            experimentSpecificRingtone, color, dismissible);
    // NotificationManager notificationManager = (NotificationManager)
    // context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    notificationManager.notify(notificationHolder.getId().intValue(), notification);

}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

private void cancelNotificationFromIntent(Intent intent) {
    if (!intent.hasExtra(EXTRA_NOTIFICATION_ID)) {
        return;//from   w  w  w  .  jav  a2  s  .  com
    }
    int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.cancel(notificationId);
}