Java Code Examples for android.app.NotificationManager#notifyAsUser()

The following examples show how to use android.app.NotificationManager#notifyAsUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Vpn.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateAlwaysOnNotification(DetailedState networkState) {
    final boolean visible = (mAlwaysOn && networkState != DetailedState.CONNECTED);

    final UserHandle user = UserHandle.of(mUserHandle);
    final long token = Binder.clearCallingIdentity();
    try {
        final NotificationManager notificationManager = NotificationManager.from(mContext);
        if (!visible) {
            notificationManager.cancelAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED, user);
            return;
        }
        final Intent intent = new Intent();
        intent.setComponent(ComponentName.unflattenFromString(mContext.getString(
                R.string.config_customVpnAlwaysOnDisconnectedDialogComponent)));
        intent.putExtra("lockdown", mLockdown);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        final PendingIntent configIntent = mSystemServices.pendingIntentGetActivityAsUser(
                intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT, user);
        final Notification.Builder builder =
                new Notification.Builder(mContext, SystemNotificationChannels.VPN)
                        .setSmallIcon(R.drawable.vpn_connected)
                        .setContentTitle(mContext.getString(R.string.vpn_lockdown_disconnected))
                        .setContentText(mContext.getString(R.string.vpn_lockdown_config))
                        .setContentIntent(configIntent)
                        .setCategory(Notification.CATEGORY_SYSTEM)
                        .setVisibility(Notification.VISIBILITY_PUBLIC)
                        .setOngoing(true)
                        .setColor(mContext.getColor(R.color.system_notification_accent_color));
        notificationManager.notifyAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED,
                builder.build(), user);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
}
 
Example 2
Source File: Tethering.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
protected void showTetheredNotification(int id, boolean tetheringOn) {
    NotificationManager notificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
        return;
    }
    int icon = 0;
    switch(id) {
      case SystemMessage.NOTE_TETHER_USB:
        icon = com.android.internal.R.drawable.stat_sys_tether_usb;
        break;
      case SystemMessage.NOTE_TETHER_BLUETOOTH:
        icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth;
        break;
      case SystemMessage.NOTE_TETHER_GENERAL:
      default:
        icon = com.android.internal.R.drawable.stat_sys_tether_general;
        break;
    }

    if (mLastNotificationId != 0) {
        if (mLastNotificationId == icon) {
            return;
        }
        notificationManager.cancelAsUser(null, mLastNotificationId,
                UserHandle.ALL);
        mLastNotificationId = 0;
    }

    Intent intent = new Intent();
    intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
            null, UserHandle.CURRENT);

    Resources r = Resources.getSystem();
    final CharSequence title;
    final CharSequence message;

    if (tetheringOn) {
        title = r.getText(com.android.internal.R.string.tethered_notification_title);
        message = r.getText(com.android.internal.R.string.tethered_notification_message);
    } else {
        title = r.getText(com.android.internal.R.string.disable_tether_notification_title);
        message = r.getText(com.android.internal.R.string.disable_tether_notification_message);
    }

    if (mTetheredNotificationBuilder == null) {
        mTetheredNotificationBuilder =
                new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS);
        mTetheredNotificationBuilder.setWhen(0)
                .setOngoing(true)
                .setColor(mContext.getColor(
                        com.android.internal.R.color.system_notification_accent_color))
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setCategory(Notification.CATEGORY_STATUS);
    }
    mTetheredNotificationBuilder.setSmallIcon(icon)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(pi);
    mLastNotificationId = id;

    notificationManager.notifyAsUser(null, mLastNotificationId,
            mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL);
}
 
Example 3
Source File: PreBootBroadcaster.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void handleMessage(Message msg) {
    final Context context = mService.mContext;
    final NotificationManager notifManager = context
            .getSystemService(NotificationManager.class);
    final int max = msg.arg1;
    final int index = msg.arg2;

    switch (msg.what) {
        case MSG_SHOW:
            final CharSequence title = context
                    .getText(R.string.android_upgrading_notification_title);

            final Intent intent = new Intent();
            intent.setClassName("com.android.settings",
                    "com.android.settings.HelpTrampoline");
            intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");

            final PendingIntent contentIntent;
            if (context.getPackageManager().resolveActivity(intent, 0) != null) {
                contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
            } else {
                contentIntent = null;
            }

            final Notification notif =
                    new Notification.Builder(mService.mContext,
                            SystemNotificationChannels.UPDATES)
                    .setSmallIcon(R.drawable.stat_sys_adb)
                    .setWhen(0)
                    .setOngoing(true)
                    .setTicker(title)
                    .setColor(context.getColor(
                            com.android.internal.R.color.system_notification_accent_color))
                    .setContentTitle(title)
                    .setContentIntent(contentIntent)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setProgress(max, index, false)
                    .build();
            notifManager.notifyAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING, notif,
                    UserHandle.of(mUserId));
            break;

        case MSG_HIDE:
            notifManager.cancelAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING,
                    UserHandle.of(mUserId));
            break;
    }
}