Android通知,震动但没有声音

Mar*_*nez 1 notifications android notificationmanager

我已经阅读了关于这个主题的其他一些帖子,我认为我的代码听起来应该是警报,但事实并非如此.它确实振动,但没有声音.关于如何传达声音的任何建议?

该程序的另一部分是能够播放铃声所以问题似乎是具体的这个例程.

这是一个扩展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);
}
Run Code Online (Sandbox Code Playgroud)

小智 5

试试这个:

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);
Run Code Online (Sandbox Code Playgroud)

还要检查这些:棒棒糖通知功能,AudioAttributes在新的通知系统的一类,和音频的使用属性.