Android O - Notification Channels - Change Vibration Pattern or Sound Type

前端 未结 5 1640
刺人心
刺人心 2020-12-13 19:57

With Android O we get the \"Notification Channels\".

As far as I understand that means that the user can\'t set

5条回答
  •  天涯浪人
    2020-12-13 20:07

    It is not a good idea to play the sound manually. The best approach would be to have two (or more) channels- one for each sound/vibration you would like to play.

    In code you can decide which channel to use depending on which sound you would like to play.

    Here is my code where I play either the default notification sound or a custom sound depending on the client's settings. The code also takes care of devices running Android prior to API 26:

    String sound = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("NotificationsSound", getString(R.string.settingsNotificationSiren));
    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.siren);
    NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel mChannel;
    String channel_id = Utils.CHANNEL_DEFAULT_ID;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (sound.toLowerCase().equals(getString(R.string.settingsNotificationSiren).toLowerCase())) {
            channel_id = Utils.CHANNEL_SIREN_ID;
            mChannel = new NotificationChannel(Utils.CHANNEL_SIREN_ID, Utils.CHANNEL_SIREN_NAME, NotificationManager.IMPORTANCE_HIGH);
            mChannel.setLightColor(Color.GRAY);
            mChannel.enableLights(true);
            mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            mChannel.setSound(soundUri, audioAttributes);
        } else {
            mChannel = new NotificationChannel(Utils.CHANNEL_DEFAULT_ID, Utils.CHANNEL_DEFAULT_NAME, NotificationManager.IMPORTANCE_HIGH);
            mChannel.setLightColor(Color.GRAY);
            mChannel.enableLights(true);
            mChannel.setDescription(Utils.CHANNEL_DEFAULT_DESCRIPTION);
        }
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel( mChannel );
        }
    }
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channel_id)
            .setSmallIcon(R.drawable.ic_stat_maps_local_library)
            .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
            .setTicker(title)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setAutoCancel(true)
            .setLights(0xff0000ff, 300, 1000) // blue color
            .setWhen(System.currentTimeMillis())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        if (sound.toLowerCase().equals(getString(R.string.settingsNotificationSiren).toLowerCase())) {
            mBuilder.setSound(soundUri);
        } else {
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
    }
    
    int NOTIFICATION_ID = 1; // Causes to update the same notification over and over again.
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
    

提交回复
热议问题