builder.addAction(android.R.drawable.ic_delete, res.getString(R.string.notification_action_exit), PendingIntent.getService(this, 0, exitIntent, 0)); R.string.notification_action_wake_lock); int actionIcon = wakeLockHeld ? android.R.drawable.ic_lock_idle_lock : android.R.drawable.ic_lock_lock; builder.addAction(actionIcon, actionTitle, PendingIntent.getService(this, 0, toggleWakeLockIntent, 0));
@Test @Config(minSdk = JELLY_BEAN_MR2) public void build_addsActionToNotification() throws Exception { PendingIntent action = PendingIntent.getBroadcast(ApplicationProvider.getApplicationContext(), 0, null, 0); Notification notification = builder.addAction(0, "Action", action).build(); assertThat(notification.actions[0].actionIntent).isEqualTo(action); }
private Notification createNotification(boolean failure) { Notification.Builder notificationBuilder = createNotificationBuilder(); notificationBuilder.setContentTitle(context.getString(R.string.app_name)); if (failure) { notificationBuilder.setContentText(context.getString(R.string.relay_disconnected)); notificationBuilder.setSmallIcon(R.drawable.ic_report_problem_24dp); } else { notificationBuilder.setContentText(context.getString(R.string.relay_connected)); notificationBuilder.setSmallIcon(R.drawable.ic_usb_24dp); } notificationBuilder.addAction(createStopAction()); return notificationBuilder.build(); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void jbNotificationExtras(boolean lowpriority, Notification.Builder builder) { // Notification try { if (lowpriority) { Method setpriority = builder.getClass().getMethod("setPriority", int.class); // PRIORITY_MIN == -2 setpriority.invoke(builder, -2); Method setUsesChronometer = builder.getClass().getMethod("setUsesChronometer", boolean.class); setUsesChronometer.invoke(builder, true); } Intent disconnectVPN = new Intent(this, DisconnectVPNActivity.class); disconnectVPN.setAction(DISCONNECT_VPN); PendingIntent disconnectPendingIntent = PendingIntent.getActivity(this, 0, disconnectVPN, 0); builder.addAction(R.drawable.ic_menu_close_clear_cancel, getString(R.string.cancel_connection), disconnectPendingIntent); Intent pauseVPN = new Intent(this, OpenVPNService.class); if (mDeviceStateReceiver == null || !mDeviceStateReceiver.isUserPaused()) { pauseVPN.setAction(PAUSE_VPN); PendingIntent pauseVPNPending = PendingIntent.getService(this, 0, pauseVPN, 0); builder.addAction(R.drawable.ic_menu_pause, getString(R.string.pauseVPN), pauseVPNPending); } else { pauseVPN.setAction(RESUME_VPN); PendingIntent resumeVPNPending = PendingIntent.getService(this, 0, pauseVPN, 0); builder.addAction(R.drawable.ic_menu_play, getString(R.string.resumevpn), resumeVPNPending); } //ignore exception } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException e) { VpnStatus.logException(e); } }
.setColor(context.getResources().getColor(R.color.colorAccent)); notificationBuider.setPriority(Notification.PRIORITY_HIGH).setVibrate(new long[0]); notificationBuider.addAction(downloadAction);
private void createNotification() { Log.i(TAG, "Create running notification"); // Create open and pause action intents Intent openIntent = new Intent(this, MainActivity.class); Intent pauseIntent = new Intent(this, ActionReceiver.class); pauseIntent.setAction(Constants.ACTION_UPDATE_STATUS); pauseIntent.putExtra(Constants.Extra.ACTION, Constants.Action.PAUSE); pauseIntent.putExtra(Constants.Extra.BRIGHTNESS, mBrightness); Notification.Action pauseAction = new Notification.Action( R.drawable.ic_wb_incandescent_black_24dp, getString(R.string.notification_action_turn_off), PendingIntent.getBroadcast(getBaseContext(), 0, pauseIntent, Intent.FILL_IN_DATA) ); // Create notification Notification.Builder builder = new Notification.Builder(getApplicationContext()) .setContentTitle(getString(R.string.notification_running_title)) .setContentText(getString(R.string.notification_running_msg)) .setSmallIcon(R.drawable.ic_brightness_2_white_36dp) .addAction(pauseAction) .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT)) .setAutoCancel(false) .setOngoing(true) .setOnlyAlertOnce(true) .setShowWhen(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(Constants.NOTIFICATION_CHANNEL_ID_RS); } mNotification = builder.build(); }
PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(android.R.drawable.ic_lock_power_off, getString(R.string.shutdown_action), shutdownPendingIntent);
private void showTimerDoneNotification() { // Cancel the countdown notification to show the "timer done" notification. cancelCountdownNotification(); // 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. NotificationManager notifyMgr = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); Notification notif = new Notification.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); }
public void noto2() // paste in activity { Notification.Builder notif; NotificationManager nm; notif = new Notification.Builder(getApplicationContext()); notif.setSmallIcon(R.drawable.back_dialog); notif.setContentTitle(""); Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notif.setSound(path); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent yesReceive = new Intent(); yesReceive.setAction(AppConstant.YES_ACTION); PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes); Intent yesReceive2 = new Intent(); yesReceive2.setAction(AppConstant.STOP_ACTION); PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2); nm.notify(10, notif.getNotification()); }
private static void addAction( Context ctxt, Notification.Builder builder, PendingIntent pendingIntent, int text, int iconId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // TODO check icon for android 7 Icon icon = Icon.createWithResource(ctxt, iconId); Notification.Action stopAction = new Notification.Action.Builder( icon, ctxt.getString(text), pendingIntent).build(); builder.addAction(stopAction); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { builder.addAction( iconId, ctxt.getString(text), pendingIntent); } }
private Notification.Builder createShareNotificationBuilder(String file) { Intent sharingIntent = buildSharedIntent(this, new File(file)); Intent chooserIntent = Intent.createChooser(sharingIntent, null); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); long timeElapsed = SystemClock.elapsedRealtime() - startTime; Intent open = buildOpenIntent(this, new File(file)); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, open, PendingIntent.FLAG_CANCEL_CURRENT); return new Notification.Builder(this) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_stat_device_access_video) .setContentTitle(getString(R.string.recording_ready_to_share)) .setContentText(getString(R.string.video_length, DateUtils.formatElapsedTime(timeElapsed / 1000))) .addAction(R.drawable.ic_share, getString(R.string.share), PendingIntent.getActivity(this, 0, chooserIntent, PendingIntent.FLAG_CANCEL_CURRENT)) .setContentIntent(contentIntent); }
private static Notification buildNotification(Context context, String filename, int recordFlag,boolean stopFlag,boolean playFlag) { PendingIntent pIntent = createPendingIntent(REQUEST_CODE_ACTIVIY, context, false); PendingIntent delIntent = getDeleteIntent(context); // Build notification // Actions are just fake int drawRecord =R.drawable.ic_record_audio; Notification.Builder builder = new Notification.Builder(context.getApplicationContext()) .setContentTitle(RECORD_AUDIO) .setContentText(filename).setSmallIcon(R.drawable.ic_audio_list) .setContentIntent(pIntent) .setDeleteIntent(delIntent); builder = builder.addAction(drawRecord, RECORD, createPendingIntent(REQUEST_CODE_SERVICE_RECORD, context, true)); builder = builder.addAction(R.drawable.ic_stop_white, STOP, createPendingIntent(REQUEST_CODE_SERVICE_STOP, context, true)); builder = builder.addAction(R.drawable.ic_play_white, PLAY, createPendingIntent(REQUEST_CODE_SERVICE_PLAY, context, true)); return builder.build(); }
private void showNotification() { final Intent notificationIntent = new Intent(Common.ACTION_STOP_CAST); PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher) .setDefaults(Notification.DEFAULT_ALL) .setOnlyAlertOnce(true) .setOngoing(true) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.casting_screen)) .addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.action_stop), notificationPendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NT_ID_CASTING, builder.build()); }
/** * Adds an action to {@code builder} using a {@code Bitmap} if a bitmap is provided and the API * level is high enough, otherwise a resource id is used. */ @SuppressWarnings("deprecation") // For addAction(int, CharSequence, PendingIntent) @TargetApi(Build.VERSION_CODES.M) // For the Icon class. protected static void addActionToBuilder(Notification.Builder builder, Action action) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.iconBitmap != null) { Icon icon = Icon.createWithBitmap(action.iconBitmap); builder.addAction( new Notification.Action.Builder(icon, action.title, action.intent).build()); } else { builder.addAction(action.iconId, action.title, action.intent); } }
private Notification.Builder createNotificationBuilder() { Notification.Builder builder = new Notification.Builder(this) .setOngoing(true) .setSmallIcon(R.drawable.ic_stat_device_access_video) .setContentTitle(getString(R.string.recording)); Intent stopRecording = new Intent(ACTION_STOP_SCREENCAST); stopRecording.setClass(this, RecBridgeService.class); builder.addAction(R.drawable.ic_stop, getString(R.string.stop), PendingIntent.getService(this, 0, stopRecording, PendingIntent.FLAG_UPDATE_CURRENT)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setChannelId(NOTIFICATION_CHANNEL_ID); } return builder; }
public static void addAction(Notification.Builder b, NotificationCompatBase.Action action) { Notification.Action.Builder actionBuilder = new Notification.Action.Builder( action.getIcon(), action.getTitle(), action.getActionIntent()); if (action.getRemoteInputs() != null) { for (RemoteInput remoteInput : RemoteInputCompatApi20.fromCompat( action.getRemoteInputs())) { actionBuilder.addRemoteInput(remoteInput); } } if (action.getExtras() != null) { actionBuilder.addExtras(action.getExtras()); } b.addAction(actionBuilder.build()); }
public static Bundle writeActionAndGetExtras( Notification.Builder builder, NotificationCompatBase.Action action) { builder.addAction(action.getIcon(), action.getTitle(), action.getActionIntent()); Bundle actionExtras = new Bundle(action.getExtras()); if (action.getRemoteInputs() != null) { actionExtras.putParcelableArray(EXTRA_REMOTE_INPUTS, RemoteInputCompatJellybean.toBundleArray(action.getRemoteInputs())); } return actionExtras; }
static Notification.Builder createBuilder(NotificationCompat2.Builder b) { final Notification.Builder builder = NotificationCompatICS.createBuilder(b); if (b.mActionIcons != null) { final int size = b.mActionIcons.size(); for (int i = 0; i < size; i++) { builder.addAction(b.mActionIcons.get(i), b.mActionTitles.get(i), b.mActionIntents.get(i)); } } return builder.setPriority(b.mPriority) .setSubText(b.mSubText) .setUsesChronometer(b.mUsesChronometer); }
private Notification.Builder getNotificationBuilder(final PendingIntent pIntent, final String text, final int icon) { return new Notification.Builder(this) .setContentTitle("HTTPServer") .setContentText(text) .setSmallIcon(icon) .setContentIntent(pIntent) .setOngoing(true) .addAction(R.drawable.online, "Open", pIntent); }