Android 8.1通知显示但没有声音

5

我有一个在前台运行的服务,其中包含一个秒表等内容。当服务启动时,通知会出现;当它结束时,我会用声音和一些文本来更新它,表示它已经完成。问题是,这在api 25中运行良好,但在26和27中更新了通知,但没有发出声音。以下是相关代码:

在服务内创建通知:

mBuilder = new NotificationCompat.Builder(context, "Main")
            .setSmallIcon(R.drawable.ic_notification_temp)
            .setContentTitle(step.getDescription())
            .setContentText(getString(R.string.notification_text))
            .setVisibility(VISIBILITY_PUBLIC)
            .setOngoing(true);
            .setDeleteIntent(deletePendingIntent);
            .setContentIntent(contentPendingIntent);
            .addAction(R.drawable.ic_stop_red_24dp, getString(R.string.cancel),finnishPendingIntent);

mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());

“工作”完成时通知构建器的更新:


mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));        
mBuilder.setVibrate(new long[]{0, 300, 0, 400, 0, 500});
mBuilder.setOngoing(false);
mBuilder.setAutoCancel(true);
mBuilder.setContentText(getString(R.string.notification_finished_text));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mBuilder.setChannelId("Sound");
    mBuilder.setCategory(NotificationCompat.CATEGORY_ALARM);
}
mNotificationManager.notify(mId, mBuilder.build());

应用启动时创建了专门为API 26或更高版本设计的2个通道:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the normal NotificationChannel
    CharSequence name = getString(R.string.app_name);
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel channel = new NotificationChannel("Main", name, importance);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);

    // Create theNotificationChannel with sound
    importance = NotificationManager.IMPORTANCE_HIGH;
    name = getString(R.string.notification_channel_sound);
    NotificationChannel sound = new NotificationChannel("Sound", name, importance);
    sound.enableVibration(true);
    sound.setVibrationPattern(new long[]{0, 300, 0, 400, 0, 500});
    AudioAttributes aa = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
            .build();
    sound.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), aa);
    notificationManager.createNotificationChannel(sound);
}

目前我的情况是这样的。我尝试着不使用setsound(),因为我读过通知应该只播放带有适当重要性的默认声音(当然在尝试之间,我甚至卸载了应用程序以正确更新频道设置),但在api 26上似乎没有任何作用,我不知道我做错了什么。


当您安装应用程序时,在通知设置中看到了什么?在显示通知时,您的活动是否正在运行? - Viktor Yakunin
你有没有查看过https://dev59.com/MlYO5IYBdhLWcg3wU_6M#46192246? - Mohammad Tabbara
@MohammadTabbara 是的,我尝试过这样做来测试是否仅仅是模拟器没有默认通知声音的问题,但是它听起来很好。但是这种解决方法也有它自己的问题... - TXinTXe
再次查看“设置”->“应用和通知”->[您的应用程序]->“应用程序通知”,您看到了什么? - Viktor Yakunin
@ViktorYakunin 首先,通知已开启,通知点也亮起来了,然后在分类中有两个,主要的没有声音并且被选中,警报的会发出声音并弹出屏幕也被选中。我认为这些都是正确的。 - TXinTXe
显示剩余5条评论
2个回答

2

我遇到了许多模拟器AVD的同样问题。下面是解决方法:

  • 启动出现问题的AVD
  • 长按AVD的电源按钮
  • 重新启动AVD

之后应该就可以正常工作了。


1

我尝试复现您的问题,并创建了MVP,也许这可以帮助您找到问题:

活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View btn = findViewById(R.id.clickMe);
        btn.setTag(NotifService.CHANNEL_ID_MAIN);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String channelId = (String) v.getTag();
                Intent intent = new Intent(v.getContext(), NotifService.class);
                intent.putExtra(NotifService.TAG, channelId);
                if (channelId.equals(NotifService.CHANNEL_ID_MAIN)) {
                    v.setTag(NotifService.CHANNEL_ID_SOUND);
                } else {
                    v.setTag(NotifService.CHANNEL_ID_MAIN);
                }
                v.getContext().startService(intent);
            }
        });
    }
}

服务:

public class NotifService extends IntentService {
    public static final String TAG = "NotificationService";
    public static final String CHANNEL_ID_MAIN = "Main";
    public static final String CHANNEL_ID_SOUND = "Sound";
    public static final int NOTIFICATION_ID = 123;

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     */
    public NotifService() {
        super(TAG);//Used to name the worker thread, important only for debugging.
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID_MAIN, "Channel Main", NotificationManager.IMPORTANCE_LOW);
        NotificationChannel sound = new NotificationChannel(CHANNEL_ID_SOUND, "Channel Sound", NotificationManager.IMPORTANCE_HIGH);
            sound.enableVibration(true);
            sound.setVibrationPattern(new long[]{0, 300, 0, 400, 0, 500});
            AudioAttributes aa = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
                    .build();
            sound.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), aa);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
                notificationManager.createNotificationChannel(sound);
            }
        }
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String channelId = intent.getStringExtra(TAG);
        showNotification(channelId);
    }

    private void showNotification(String channelId) {
        boolean inProgress = channelId.equals(CHANNEL_ID_MAIN);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Work")
                .setContentText(inProgress ? "InProgress" : "Finished")
                .setOngoing(inProgress)
                .setVisibility(VISIBILITY_PUBLIC)
                .setAutoCancel(!inProgress);

        if (!inProgress) {
            builder.setCategory(NotificationCompat.CATEGORY_ALARM);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    }
}

这在我的8.1设备和8.1模拟器上运行良好(第一次通知时无声,第二次通知在完成工作时带有震动和声音)。

1
好的。我刚刚将你的所有代码复制到一个新项目中并在这里尝试了一下,它确实是一样的,通知显示但没有声音。所以这一定是我的模拟器或其他问题...非常感谢! - TXinTXe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接