Android Notification to play sound only

后端 未结 4 2102
清歌不尽
清歌不尽 2020-12-09 12:41

I have a countdown timer in my activity and when it reaches zero I send a notification to the status bar which also plays a custom wav file to alert the user. I only want t

相关标签:
4条回答
  • 2020-12-09 13:21

    In 'Adding a sound' section in http://developer.android.com/guide/topics/ui/notifiers/notifications.html there is this note :

    Note: If the defaults field includes DEFAULT_SOUND, then the default sound overrides any sound defined by the sound field.

    So, if you want to customize only sound, you may use -

    notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
    notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
    
    0 讨论(0)
  • 2020-12-09 13:21
    NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
    

    three ways to setSound

    builder.setDefaults(Notification.DEFAULT_SOUND);
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    
    0 讨论(0)
  • 2020-12-09 13:24

    According to my experiment, you can indeed send a notification without it appear in the notification area but still have the sound played.

    All you need to do is to construct a notification builder and set sound to it, but skip the pending intent, icon, content title and content text.

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSound(your_sound_uri);
    notificationManager.notify(1234, builder.build());
    
    0 讨论(0)
  • 2020-12-09 13:31
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
    Notification mNotification = new Notification.Builder(this)
    
                .setContentTitle("New Post!")
                .setContentText("Here's an awesome update for you!")
                .setSmallIcon(R.drawable.ic_lancher)
                .setContentIntent(pIntent)
                .setSound(soundUri)
                .build();
    
    0 讨论(0)
提交回复
热议问题