安卓8.1的通知显示但不作任何声音

问题描述 投票:2回答:2

我有一个在前台与运行,除其他事项外,秒表的服务。在服务启动时出现的通知,并且当它结束时我的声音和一些文字,表明它的完成更新。问题是,这工作得很好,直到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工作,我只是不知道我做错了什么。

java android notifications
2个回答
0
投票

试图重现您的问题,并与创建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模拟器。


0
投票

我有我的大部分模拟器自动真空淀积系统完全相同的问题。下面的固定对我来说:

  • 启动受影响的AVD
  • 长按AVD的电源按钮
  • 重新启动AVD

此后,它应该再工作。

© www.soinside.com 2019 - 2024. All rights reserved.