android: stop notification from vibrating - android

Hello i am building a notification with
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this, "default")
.setShowWhen(false)
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0, 1, 2))
.setColor(getResources().getColor(R.color.colorPrimary))
.setSmallIcon(android.R.drawable.stat_sys_headset)
.setContentText(activeAudio.getArtist())
.setContentTitle(activeAudio.getAlbum())
.setContentInfo(activeAudio.getTitle())
.addAction(android.R.drawable.ic_media_previous, "previous", playbackAction(3))
.addAction(notificationAction, "pause", play_pauseAction)
.addAction(android.R.drawable.ic_media_next, "next", playbackAction(2));
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notificationBuilder.build());
and i don't want the phone to vibrate whenever it's created. I've tried
.setVibrate(null)
.setVibrate(new Long[]{0l})
.setDefaults(Notification.DEFAULT_SOUND)
in all combinations, but with no effect. Any Ideas?

You must uninstall the application and reinstall
Because it saves the settings from the first session
As presented in this responsum
For Android 8 or higher
try it
mNotificationChannel.setVibrationPattern(new long[]{ 0 });
mNotificationChannel.enableVibration(true);

Related

How to set device to silent mode without vibration

My app has a certain functionality in which I need to set device to silent mode without vibration. This is triggered by the arrival of a notification.
I used the following code to do this:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
This works when the device screen is ON, but does not work many times, when the device screen is OFF (when it is left idle) for some time.
Is there any way to make this work everytime, even when the phone screen is OFF?
Try this
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this);
this will lead to a pattern of vibrate use the notification Builder and remove the set vibrate, this will disable the vibration.
Example of handling the sound and vibrate:
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(body)
.setContentTitle(title)
.setSound(soundUri)
.setLargeIcon(icon)
.setSound(soundUri)
.setLights(Color.parseColor("#ffb400"), 50, 10)
.setVibrate(new long[]{500, 500, 500, 500})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

Notifications in Moto Display

I created a notification and it works but not display in Moto Display when locked.
I changed Priority, Category etc with no effects.
I want this notification like messages or missed cals:
moto display
This runs as a service:
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.emblem2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_help_red))
.setColor(getResources().getColor(R.color.colorFirst))
.setContentTitle(title)
.setContentText(location)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setShowWhen(true)
.setCategory(Notification.CATEGORY_ALARM)
.setLights(Color.YELLOW, 200, 200)
.setContentIntent(pendingIntent);
notificationBuilder.setVibrate((sharedPreferences.getBoolean("notifications_new_message_vibrate", false) ? new long[]{0, 300, 200, 300} : new long[]{}));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, notificationBuilder.build());
As pointed out by Marcin, Moto Display doesn't work with vector drawable in notification small icon. I had the same problem and changing the small icon resolved the issue.
It's sad that such a nice feature as Moto Display doesn't have a documentation pointing that out.
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button */)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Wonderful music")
.setContentText("My Awesome Band")
.setLargeIcon(albumArtBitmap)
.build();
That worked in my Moto Z, maybe only work in Media mode.

setgroup() in notification not working

I'm tring to create notification group, this is my code:
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("New mail from " + 1)
.setContentText("cv")
.setSmallIcon(R.drawable.rh_logo)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faaborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("johndoe#gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
// Issue the notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
When I run the app, and send notification messages, they do not show in group.
Can someone explain me what I need to change?
You have to create a group notification before creating your custom notification. Just like this:
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setGroupSummary(true)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent);
Do not forget setGroupSummary to true.
Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
.setContentTitle(title)
.setContentText(content)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent)
Finally use NoticationManagerCompat to notify them.
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(GROUP_ID, groupBuilder.build());
manager.notify(id, builder.build());
Replace
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
with
NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);
because you are using NotificationCompat
There is a strange behavior of notification. If you add autocancel notification group doesn't work, if you add setGroup or setGroupSummary. It will not work. Removing all that fixed the problem for me. Basically, you don't need to mention any group it will auto group. TESTED on Redmi 10 pr0, Poco x3 NFC, huawei device.
return new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_LOW);

Update Notification without Collasping notification drawer

i have a notification manager that launches an activity when clicked.
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setAutoCancel(false);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Launch.class).putExtra("type", 0), 0);
mBuilder.setContentIntent(pendingIntent);
the function performed in this can also be be asynchronous depending on the users preference. I want to update the notification to an ongoing progress. something of this nature:
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle("Sending details")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setProgress(100, 0, true)
.setOnlyAlertOnce(true)
.setAutoCancel(false);
when the notification is selected. This is achieved but what i want to do is update the notification from the former to the later without having to first slide up the navigation drawer.
Try this..
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
you need NOTIFICATION_ID to updated the specific notification.
Please refer this

How to set notification to clear itself on click?

How to set my notification to clear itself on click?
I've set autoCancel(true) but it doesn't work
My code:
Notification n = new Notification.Builder(this)
.setContentTitle("Update for you")
.setContentText("Please click here to see the update information")
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pList)
.setAutoCancel(true)
.addAction(R.drawable.ic_read, "Read", pRead)
.addAction(R.drawable.ic_list, "All Updates", pList)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
setAutoCancel() of Notification.Builder is introduced in API 11. Perhaps, your minimum version is lower than API 11.
setAutoCancel() is also available in NotificationCompat.Builder class which is added in Support library
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("Update for you")
.setContentText("Please click here to see the update information")
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pList)
.setAutoCancel(true)
.addAction(R.drawable.ic_read, "Read", pRead)
.addAction(R.drawable.ic_list, "All Updates", pList)
.build();
Use FLAG_AUTO_CANCEL
n.flags |= Notification.FLAG_AUTO_CANCEL;
SetAutoCancel(true)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Quiz Tap")
.setContentText(messageBody)
/*.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))*//*Notification with Image*/
**.setAutoCancel(true)**
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

Categories

Resources