1

I am using AlarmManager in my project, since Android 10 doesn't allow startActivity in backround, for >Android 10 I start a forgroundservice. All works fine except I am not able to change notification sound, I want to set a custom sound or default device alarm sound, I tried almost all suggestions but unfortunately it doesn't work. My codes are below,

public class AlertBroadcastReceiver extends BroadcastReceiver {

@Override
// This method is called when the alarm is fired (alarm calacagi zaman burasi trigger olur)
public void onReceive(Context context, Intent intent) {

    if (intent.getBooleanExtra("isAlarmSet", false)) {
       
      
        if (Build.VERSION.SDK_INT >= 29) {

            Intent serviceIntent = new Intent(context, AlarmForegroundService.class);
            serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            serviceIntent.putExtra("requestCode", intent.getIntExtra("requestCode", 0));
            context.startForegroundService(serviceIntent);

            return;
        }


        Intent intentNew = new Intent(context, ActivityAlarmScreen.class);
        intentNew.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentNew.putExtra("requestCode", intent.getIntExtra("requestCode", 0));
        context.startActivity(intentNew);

    }

}

and service class;

public class AlarmForegroundService extends Service {
private static final String TAG = "Forgroundervice";

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int requestCode = intent.getIntExtra("requestCode", 0);

    Intent alarmIntent = new Intent(this, ActivityAlarmScreen.class);
    alarmIntent.putExtra("requestCode", requestCode);
    PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0,
            alarmIntent, 0);

    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.alarm);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "alarm service", NotificationManager.IMPORTANCE_HIGH);

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

        serviceChannel.setSound(soundUri, audioAttributes);
        serviceChannel.enableLights(true);
        serviceChannel.enableVibration(true);

        NotificationManager manager = getSystemService(NotificationManager.class);
        if (manager != null) {
            manager.createNotificationChannel(serviceChannel);
        }
    }

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon_lap_true)
                    .setContentTitle("Alarm is ringing")
                    .setContentText("Press to snooze or to dismiss the alarm")
                    .setAutoCancel(true)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setSound(soundUri)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(alarmPendingIntent);

    Notification alarmNotification = notificationBuilder.build();

    startForeground(requestCode, alarmNotification);

    return START_NOT_STICKY;
}}
Ahmet
  • 266
  • 3
  • 9

0 Answers0