How to set device to silent mode without vibration - android

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());

Related

android: stop notification from vibrating

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);

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

Notification does not vibrate when triggered

I am working on an app in android in which i wish to notify specific users about specific events. These notifications are made by using the the Firebase Cloud Messaging data-messages. When I send a data-message to the users client, the onMessageReceived-method is called and I can handle these messages as I wish.
Now I want to make the device vibrate in the very moment the messages arrives. Therefore, I tried to build a notification and set the vibration on it, however it does not vibrate at all..
I included the VIBRATE-permission in my applications Manifest as well.
Here is my Code to build the notification:
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_icon)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setVibrate(new long[] {500,500,500,500,500,500,500,500,500});
Notification note = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
Have I missed anything?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic)
.setContentTitle(context.getString(R.string.text))
.setContentText(context.getString(R.string.app_name))
.setContentIntent(contentIntent);
mBuilder.setVibrate(new long[]{500, 500});
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, mBuilder.build());
This works for me.

My FCM notification does not expand when the app is in background

I am implementing FCM to push notifications, but I am trying to expand the notification when the content of the notification is big.
It cannot be implemented when the app is in background
here is my code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_new_logo).setTicker(title).setWhen(0)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_new_logo))
.setContentTitle("SuezApp")
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
final NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(notificationBuilder);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
How can I expand it when the app is in background?
Per the Firebase 9.4 release notes:
Expanded gestures are now supported for messages, allowing the Android UI to display multiple lines when the body of a notification exceeds a single line.
Make sure you are using Firebase 9.4 or higher as your dependency.

NotificationCompat.Builder with expand notification is not working

I am trying to expand notification if text is long by below code.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
but it shows single line notification and cannot expand it. My testing device is the Marshmallow.
Using setpriority might help-
.setPriority(Notification.PRIORITY_MAX)

Categories

Resources