Not getting any sound or vibration on notification - android

When the notification is received I dont get the sound and the vibration. For information phone is not in mute mode.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
long[] pattern = {500,500,500};
builder.setVibrate(pattern);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());

Related

how to make heads-up notifications in android studio?

As I've searched how to reveal heads-up, I found a solution that is to set priority high but it still doesn't show heads-ups. I've got a notfication massenger service. Its function is as below. Is there anything I missed in settings?
Heading
mycode:
private void sendNotification(String body, String title) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
remoteViews.setTextViewText(R.id.title, "Firebase");
remoteViews.setTextViewText(R.id.body, body);
remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setCustomHeadsUpContentView(remoteViews)
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker(body)
.setSound(notificationSound)
.setFullScreenIntent(pendingIntent, true)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notifBuilder.build());
}
Try replacing .setContentIntent(pendingIntent) with .setFullScreenIntent(pendingIntent, true) in your Notification Builder.

How to play notification sound from local file

sound = Uri.parse("file://" + audioFilePath.getAbsolutePath());
/*sound = Uri.fromFile(audioFilePath);*/
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(resultPendingIntent)
.setSound(sound);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mBuilder.setPriority(Notification.PRIORITY_HIGH);
}
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
The sound file is present in local storage. Notification working properly but sound doesn't play. I tried both way to parse Uri from file path.
If you put your sound in your resource folder (which you should do anyway for notification sounds) and use Notification.Builder instead of NotificationCompat.Builder, you should be able to access it like this:
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
getPackageName() + "/" + R.raw.name_of_sound);
Notification.Builder mBuilder =
new Notification.Builder(this);
mBuilder
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(resultPendingIntent)
.setSound(sound);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mBuilder.setPriority(Notification.PRIORITY_HIGH);
}
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
Source

Android, notification sound while app is minimized in android?

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_medico_launcher)
.setContentTitle(title)
.setContentText(msgBody)
.setAutoCancel(true)
.setGroup(group)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msgBody))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId++, notificationBuilder.build());

Android notification sound doesn't work

var notificationBuilder = new Notification.Builder(context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.Icon))
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetStyle(new Notification.BigTextStyle().BigText(message))
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000 })
.SetDefaults(NotificationDefaults.All);
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notificationBuilder.Build());
Where i was wrong? Why my notifications doesn't have any sound or vibration?
Remove .SetDefaults(NotificationDefaults.All), you are resetting the notification settings that you just built:
var notification = new Notification.Builder(Application.Context)
.SetSmallIcon(Resource.Mipmap.Icon)
.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Mipmap.Icon))
.SetContentTitle("Stack")
.SetContentText("OverFlow")
.SetAutoCancel(true)
//.SetContentIntent(pendingIntent)
.SetStyle(new Notification.BigTextStyle().BigText("OverFlow"))
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000 })
.Build();
var notificationManager = (NotificationManager)Application.Context.GetSystemService(Context.NotificationService);
notificationManager.Notify(1, notification);
Try passing uri to setSound in notificationBuilder like this
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(uri);

Notification style error when bigtext

i am showing notification from following code but problem is when text is small it not showing but when text is big then it showing
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.app_icon).setContentTitle(title).setAutoCancel(true).setContentText(body)
.setContentIntent(contentIntent);
mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(body));
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(soundUri); // notification sound
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = Integer.valueOf(last4Str);
mNotificationManager.notify(notificationId, mBuilder.build());

Categories

Resources