Custom Notification Sound not working in Android Oreo

前端 未结 6 1680
闹比i
闹比i 2020-12-14 16:44
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + \"://\" + context.getPackageName() + \"/\" + R.raw.notification_mp3);
            mBuilder.setSound(s         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 17:26

    This might help the new comers.

    Here is a code for a working notification sample on all android versions with all kinds of possible setups.

    1 -> sound and vibration
    2 -> sound but no vibration
    3 -> no sound but vibration
    4 -> no sound no vibration
    

    OUTPUT

    Github Repo -> https://github.com/usman14/Notification

    CODE

    MainActivity

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.NotificationCompat;
    import android.annotation.TargetApi;
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CompoundButton;
    import android.widget.Switch;
    
    public class MainActivity extends AppCompatActivity {
    
        public boolean shouldSound;
        public boolean shouldVibrate;
        NotificationManager notificationManager;
    
        Button button;
        Switch soundSwitch;
        Switch vibrationSwitch;
    
        @TargetApi(Build.VERSION_CODES.O)
        public void registerNormalNotificationChannel(android.app.NotificationManager notificationManager) {
    
            NotificationChannel channel_all = new NotificationChannel("CHANNEL_ID_ALL", "CHANNEL_NAME_ALL", NotificationManager.IMPORTANCE_HIGH);
            channel_all.enableVibration(true);
            notificationManager.createNotificationChannel(channel_all);
    
            NotificationChannel channel_sound = new NotificationChannel("CHANNEL_ID_SOUND", "CHANNEL_NAME_ALL", NotificationManager.IMPORTANCE_HIGH);
            channel_sound.enableVibration(false);
            notificationManager.createNotificationChannel(channel_sound);
    
            NotificationChannel channel_vibrate = new NotificationChannel("CHANNEL_ID_VIBRATE", "CHANNEL_NAME_ALL", NotificationManager.IMPORTANCE_HIGH);
            channel_vibrate.setSound(null, null);
            channel_vibrate.enableVibration(true);
            notificationManager.createNotificationChannel(channel_vibrate);
    
    
            NotificationChannel channel_none = new NotificationChannel("CHANNEL_ID_NONE", "CHANNEL_NAME_ALL", NotificationManager.IMPORTANCE_HIGH);
            channel_none.setSound(null, null);
            channel_none.enableVibration(false);
            notificationManager.createNotificationChannel(channel_none);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = findViewById(R.id.btn);
            soundSwitch = findViewById(R.id.switch_sound);
            vibrationSwitch = findViewById(R.id.switch_vibration);
            notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (isOreoOrAbove()) {
                setupNotificationChannels();
            }
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    makeNotification();
                }
            });
    
            soundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        shouldSound = true;
                    } else {
                        shouldSound = false;
                    }
                }
            });
    
            vibrationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        shouldVibrate = true;
                    } else {
                        shouldVibrate = false;
                    }
                }
            });
        }
    
        private void setupNotificationChannels() {
            registerNormalNotificationChannel(notificationManager);
        }
    
        public void makeNotification() {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, getChannelId())
                    .setContentTitle("Hi")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Welcome to Android");
    
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
    
            PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            builder.setContentIntent(pendingIntent);
            if (shouldSound && !shouldVibrate) {
                builder.setDefaults(Notification.DEFAULT_SOUND)
                        .setVibrate(new long[]{0L});
            }
            if (shouldVibrate && !shouldSound) {
                builder.setDefaults(Notification.DEFAULT_VIBRATE)
                        .setSound(null);
            }
            if (shouldSound && shouldVibrate) {
                builder.setDefaults(Notification.DEFAULT_ALL);
            }
    
    
            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, builder.build());
        }
    
        private String getChannelId() {
            if (shouldSound && shouldVibrate) {
                return "CHANNEL_ID_ALL";
            } else if (shouldSound && !shouldVibrate) {
                return "CHANNEL_ID_SOUND";
            } else if (!shouldSound && shouldVibrate) {
                return "CHANNEL_ID_VIBRATE";
            } else {
                return "CHANNEL_ID_NONE";
            }
        }
    
        private boolean isOreoOrAbove() {
            return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O;
        }
    }
    

    activity_main (xml)

    
    
    
    
    
        
    
            
    
    
            
    
            
    
        
    
        
    
            
    
    
            
    
            
    
        
    
    
        
    
    
    

提交回复
热议问题