Android通知,震动但没有声音

时间:2014-12-06 06:29:54

标签: android notifications notificationmanager

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

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

这是一个扩展服务

的类
@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);
}

3 个答案:

答案 0 :(得分: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);

同时检查以下内容:Lollipop notification featureAudioAttributes class used in new notification systemusage of Audio Attributes

答案 1 :(得分:0)

首先检查设备通知声音设置。

然后试试这个

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);

答案 2 :(得分:0)

试试此代码

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