Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification Sound

People also ask

Where are the notification sounds?

If you are using Google Messages, tap Notifications. Tap Sound. This should open a list of available tones on most Androids. Select a sound.


What was missing from my previous code:

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

Just put your sound file in the Res\raw\siren.mp3 folder, then use this code:

For Custom Sound:

Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.siren);

For Default Sound:

notification.defaults |= Notification.DEFAULT_SOUND;

For Custom Vibrate:

long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;

For Default Vibrate:

notification.defaults |= Notification.DEFAULT_VIBRATE;

Another way for the default sound

builder.setDefaults(Notification.DEFAULT_SOUND);

USE Can Codeding

 String en_alert, th_alert, en_title, th_title, id;
 int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage;

 class method
 Intent intent = new Intent(context, ReserveStatusActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


 intent = new Intent(String.valueOf(PushActivity.class));
 intent.putExtra("message", MESSAGE);
 TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
 stackBuilder.addParentStack(PushActivity.class);
 stackBuilder.addNextIntent(intent);
 // PendingIntent pendingIntent =
 stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

 //      android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
 //        bigStyle.bigText((CharSequence) context);



 notification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(th_title)
    .setContentText(th_alert)
    .setAutoCancel(true)

 // .setStyle(new Notification.BigTextStyle().bigText(th_alert)  ตัวเก่า
 //

 .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))

    .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))

    .setContentIntent(pendingIntent)
    .setNumber(++numMessages)


    .build();

 notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

 notificationManager.notify(1000, notification);

On Oreo (Android 8) and above it should be done for custom sound in this way (notification channels):

Uri soundUri = Uri.parse(
                         "android.resource://" + 
                         getApplicationContext().getPackageName() +
                         "/" + 
                         R.raw.push_sound_file);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build();

// Creating Channel
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
                                                      "YOUR_CHANNEL_NAME",
                                                      NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                                           .createNotificationChannel(notificationChannel);