Java Android 8.1通知显示但不发出任何声音
我有一个在前台运行的服务,其中包括一个秒表。当服务启动时,会出现通知,当服务结束时,我会用声音和一些文本来更新通知,表明服务已完成。问题是,在api 25之前,它工作得很好,但对于26和27,它会更新通知,但不会发出声音。以下是相关代码: 在服务内创建通知:Java Android 8.1通知显示但不发出任何声音,java,android,notifications,Java,Android,Notifications,我有一个在前台运行的服务,其中包括一个秒表。当服务启动时,会出现通知,当服务结束时,我会用声音和一些文本来更新通知,表明服务已完成。问题是,在api 25之前,它工作得很好,但对于26和27,它会更新通知,但不会发出声音。以下是相关代码: 在服务内创建通知: mBuilder = new NotificationCompat.Builder(context, "Main") .setSmallIcon(R.drawable.ic_notification_temp)
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似乎不起任何作用,我只是不知道我做错了什么。试图重现您的问题,并以创建的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和Emulator 8.1上运行良好(第一次单击时没有声音表示第一次通知,第二次单击时有振动+声音表示工作完成通知)。我的大多数Emulator AVD都有完全相同的问题。以下内容为我修复了它:
- 启动受影响的AVD
- 长按AVD的电源按钮
- 重新启动AVD
之后,它应该会再次工作。安装应用程序时,您在通知设置中看到了什么?显示通知时,您的活动是否正在运行?是否检查?@ViktorYakunin这两个频道创建成功,但通知是从服务运行的,尽管我试图让调用服务的活动处于活动状态,以防万一。通知显示和行为正确(首先是抽屉中的徽章,没有视觉干扰,然后更新的文本和通知显示正确,没有任何声音)。@Mohammadtabara是的,我试图这样测试是否只是模拟器没有默认通知声音的问题,但这听起来很好。但这种解决方法也存在一些问题……同样,您在设置->应用程序和通知->应用程序->应用程序通知中看到了什么?好的。我刚刚将您的所有代码复制到一个新项目中,并在这里尝试了它,它也做了同样的事情,通知显示但没有声音。所以这一定是我的仿真器出了问题或者别的什么。。。非常感谢你!奇怪的是,这解决了我的问题!谢谢