.setSmallIcon(smallIconResID) .setVibrate(new long[]{0, 250, 100, 250}) .setSound(soundUri) .setNumber(number) .setContentIntent(pendingIntent)
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 void sendNotification(String body, String title) { Intent intent = new Intent(this, Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Notification.Builder notificationBuilder = (Notification.Builder) new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }
static Notification add(Context context, Notification n, CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, RemoteViews tickerView, int number, PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon) { Notification.Builder b = new Notification.Builder(context) .setWhen(n.when) .setSmallIcon(n.icon, n.iconLevel) .setContent(n.contentView) .setTicker(n.tickerText, tickerView) .setSound(n.sound, n.audioStreamType) .setVibrate(n.vibrate) .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS) .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) .setDefaults(n.defaults) .setContentTitle(contentTitle) .setContentText(contentText) .setContentInfo(contentInfo) .setContentIntent(contentIntent) .setDeleteIntent(n.deleteIntent) .setFullScreenIntent(fullScreenIntent, (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0) .setLargeIcon(largeIcon) .setNumber(number); return b.getNotification(); } }
.setContent(n.contentView) .setTicker(n.tickerText, tickerView) .setSound(n.sound, n.audioStreamType) .setVibrate(n.vibrate) .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS)
/** * Sets the sound in the notification builder if the property is set in the intent. * This is effective only for devices running or targeting an Android version lower than 8. * * @param context The current context. * @param pushIntent The push intent. * @param builder The builder to modify. */ @SuppressWarnings("deprecation") private static void setSound(Context context, Intent pushIntent, Notification.Builder builder) { String sound = PushIntentUtils.getSound(pushIntent); if (sound != null) { try { Resources resources = context.getResources(); int id = resources.getIdentifier(sound, "raw", context.getPackageName()); Uri soundUri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(id)) .appendPath(resources.getResourceTypeName(id)) .appendPath(resources.getResourceEntryName(id)) .build(); builder.setSound(soundUri); } catch (Resources.NotFoundException e) { AppCenterLog.warn(LOG_TAG, "Sound file '" + sound + "' not found; falling back to default."); builder.setDefaults(Notification.DEFAULT_SOUND); } } }
static Notification add(Context context, Notification n, CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, RemoteViews tickerView, int number, PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon) { Notification.Builder b = new Notification.Builder(context) .setWhen(n.when) .setSmallIcon(n.icon, n.iconLevel) .setContent(n.contentView) .setTicker(n.tickerText, tickerView) .setSound(n.sound, n.audioStreamType) .setVibrate(n.vibrate) .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS) .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) .setDefaults(n.defaults) .setContentTitle(contentTitle) .setContentText(contentText) .setContentInfo(contentInfo) .setContentIntent(contentIntent) .setDeleteIntent(n.deleteIntent) .setFullScreenIntent(fullScreenIntent, (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0) .setLargeIcon(largeIcon); return b.getNotification(); } }
static Notification.Builder createBuilder(NotificationCompat2.Builder b) { final Notification n = b.mNotification; return new Notification.Builder(b.mContext) .setWhen(n.when) .setSmallIcon(n.icon, n.iconLevel) .setContent(n.contentView) .setTicker(n.tickerText, b.mTickerView) .setSound(n.sound, n.audioStreamType) .setVibrate(n.vibrate) .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS) .setOngoing((n.flags & FLAG_ONGOING_EVENT) != 0) .setOnlyAlertOnce((n.flags & FLAG_ONLY_ALERT_ONCE) != 0) .setAutoCancel((n.flags & FLAG_AUTO_CANCEL) != 0) .setDefaults(n.defaults) .setContentTitle(b.mContentTitle) .setContentText(b.mContentText) .setContentInfo(b.mContentInfo) .setContentIntent(b.mContentIntent) .setDeleteIntent(n.deleteIntent) .setFullScreenIntent(b.mFullScreenIntent, (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0) .setLargeIcon(b.mLargeIcon) .setNumber(b.mNumber); }
@Test public void handleNotificationWithCustomSound() throws Exception { String customSoundString = "sound"; int resourceId = 2; when(PushIntentUtils.getSound(any(Intent.class))).thenReturn(customSoundString); Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(customSoundString), eq("raw"), anyString())).thenReturn(resourceId); when(resourcesMock.getResourcePackageName(resourceId)).thenReturn("packageName"); when(resourcesMock.getResourceTypeName(resourceId)).thenReturn("typeName"); when(resourcesMock.getResourceEntryName(resourceId)).thenReturn("entryName"); Uri.Builder uriBuilderMock = mock(Uri.Builder.class); Uri uriMock = mock(Uri.class); whenNew(Uri.Builder.class).withNoArguments().thenReturn(uriBuilderMock); when(uriBuilderMock.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)).thenReturn(uriBuilderMock); when(uriBuilderMock.authority(resourcesMock.getResourcePackageName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.appendPath(resourcesMock.getResourceTypeName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.appendPath(resourcesMock.getResourceEntryName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.build()).thenReturn(uriMock); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSound(uriMock); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
Notification.Builder mBuilder = new Notification.Builder(this) .setContentTitle("NotificationDemo") .setContentText("NotificatinDemo") .setSmallIcon(R.drawable.ic_launcher) .setVibrate(pattern) .setContentIntent(pIntent); Notification myNote = mBuilder.build(); if(Build.VERSION.SDK_INT >= 21) { myNote.sound = sound; myNote.category = Notification.CATEGORY_ALARM; AudioAttributes.Builder attrs = new AudioAttributes.Builder(); attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION); attrs.setUsage(AudioAttributes.USAGE_ALARM); myNote.audioAttributes = attrs.build(); } else { mBuilder.setSound(sound, AudioManager.STREAM_ALARM); myNote = mBuilder.build(); } myNM.notify(1, myNote);
private Notification getNotification(String content) { Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle(getString(R.string.app_name)); builder.setContentText(content); builder.setTicker(content); builder.setSmallIcon(R.drawable.ic_notification_appicon); builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); builder.setDefaults(Notification.DEFAULT_VIBRATE); builder.setAutoCancel(true); builder.setLights(Color.GREEN, 500, 500); // This intent is fired when notification is clicked Intent intent = new Intent(getApplicationContext(), YouMainActvity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // Set the intent that will fire when the user taps the notification. builder.setContentIntent(pendingIntent); return builder.getNotification(); }
private void sendNotification(String body, String title) { Intent intent = new Intent(this, Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Notification.Builder notificationBuilder = (Notification.Builder) new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); }
private void sendNotification(String contentText) { Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); Notification n = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_stat_kd) .setContentTitle("Possible Karma attack detected!") .setContentText(contentText) .setSound(defaultNotificationUri) .setLights(Color.RED, 500, 500) .setOnlyAlertOnce(true) .setContentIntent(intent) .setAutoCancel(true).build(); notificationManager.notify(0, n); }
Notification.Builder mBuilder = new Notification.Builder(this) mBuilder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound));
public static void showNotification(String title, String content, PendingIntent intent, int notificationId, boolean sound, boolean vibrate, PendingIntent deleteIntent, Uri sound_uri) { final Notification.Builder mBuilder = notificationBuilder(title, content, intent); final long[] vibratePattern = {0, 1000, 300, 1000, 300, 1000}; if (vibrate) mBuilder.setVibrate(vibratePattern); if (deleteIntent != null) mBuilder.setDeleteIntent(deleteIntent); mBuilder.setLights(0xff00ff00, 300, 1000); if (sound) { Uri soundUri = (sound_uri != null) ? sound_uri : RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mBuilder.setSound(soundUri); } final NotificationManager mNotifyMgr = (NotificationManager) xdrip.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE); // if (!onetime) mNotifyMgr.cancel(notificationId); mNotifyMgr.notify(notificationId, mBuilder.build()); }
Notification.Builder builder = new Notification.Builder(context); builder.setSound(Uri.fromFile(yourFile)); builder.setTicker(yourMessage); NotificationManager.notify(1,builder.getNotification());
private static void configRingTone(Notification.Builder builder) { Uri uri = null; if (!TextUtils.isEmpty(SettingUtils.getRingtone())) { uri = Uri.parse(SettingUtils.getRingtone()); } if (uri != null && isSystemRinger(BeeboApplication.getInstance())) { builder.setSound(uri); } }
@Override public NotificationBridge setSound(Uri sound) { mBuilder.setSound(sound); return this; }
@Override public NotificationBridge setSound(Uri sound, int streamType) { mBuilder.setSound(sound, streamType); return this; }