小编TXi*_*TXe的帖子

Android 8.1 通知显示但不发出任何声音

我有一个在前台运行的服务,其中包括一个秒表。当服务启动时会出现通知,当它结束时,我会用声音和一些表明它已完成的文本来更新它。问题是这在 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());
Run Code Online (Sandbox Code Playgroud)

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

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

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

java notifications android

5
推荐指数
1
解决办法
1398
查看次数

标签 统计

android ×1

java ×1

notifications ×1