Android notification, vibrates but no sound

≡放荡痞女 提交于 2019-12-11 11:06:19

问题


I've read some of the other posts on this subject and I think my code should be sounding an alarm, but it's not. It does vibrate, but no sound. Any suggestions on how to get this to convey sound ?

Another part of the program is able to play a ringtone so the problem seems to be specific this routine.

This is in a class that extends Service

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

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (sound == null) { Log.i("RECEIVER", "SOUND IS NULL"); }

    NotificationManager myNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent intentMain = new Intent(this.getApplicationContext(), MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentMain, 0);

    long[] pattern = {500,500,500,500,500,500,500,500,500};

    Notification myNote = new Notification.Builder(this)
            .setContentTitle("NotificationDemo")
            .setContentText("NotificatinDemo")
            .setSmallIcon(R.drawable.ic_launcher)
            .setSound(sound)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .build();

    myNM.notify(1, myNote);
    return super.onStartCommand(intent, flags, startId);
}

回答1:


Try this:

Notification.Builder mBuilder = new Notification.Builder(this)
                .setContentTitle("NotificationDemo")
                .setContentText("NotificatinDemo")
                .setSmallIcon(R.drawable.ic_launcher)
                .setVibrate(pattern)
                .setContentIntent(pIntent);

Notification myNote = mBuilder.build();

if(Build.VERSION.SDK_INT >= 21) {
    myNote.sound = sound;
    myNote.category = Notification.CATEGORY_ALARM;

    AudioAttributes.Builder attrs = new AudioAttributes.Builder();
    attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
    attrs.setUsage(AudioAttributes.USAGE_ALARM);
    myNote.audioAttributes = attrs.build();
} else  {
    mBuilder.setSound(sound, AudioManager.STREAM_ALARM);
    myNote = mBuilder.build();
}

myNM.notify(1, myNote);

Also check these: Lollipop notification feature, AudioAttributes class used in new notification system, and usage of Audio Attributes.




回答2:


First Check your device notification sound settings.

Then try this

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(someText)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true);



回答3:


Try this code

Notification myNote = new Notification.Builder(this)
            .setContentTitle("NotificationDemo")
            .setContentText("NotificatinDemo")
            .setSmallIcon(R.drawable.ic_launcher)
            .setSound(Notification.DEFAULT_SOUND)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .build();


来源:https://stackoverflow.com/questions/27329004/android-notification-vibrates-but-no-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!