首页 > 解决方案 > 如何让通知声音持续播放并仅在单击通知的“取消通知”按钮时停止

问题描述

我有此代码用于为我的应用程序创建通知,问题是当我收到通知时,当我打开通知面板时声音立即停止!如果手机被锁定,按电源按钮唤醒它也会停止声音。我想要的是获得一个持续的通知声音,只有当我点击触发 CancelNotification.class' onReceive 的通知的“取消通知”按钮时才会停止

//这是我的 SDK > 0 的通知通道

 @TargetApi(Build.VERSION_CODES.O)
    private void createChannel(String channelDesc, Boolean isSoundDisabled, String channelID, String channelName) {
        Uri uri = null;
        AudioAttributes attributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
        if (!isSoundDisabled)
            uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.adhan);
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(channelDesc);
        channel.enableVibration(true);
        channel.setSound(uri, attributes);
        channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(channel);
    }  

//通知生成器

  public NotificationCompat.Builder getChannelNotification(String title, String description, String channelID) {
        PendingIntent mainIntentPending = PendingIntent.getActivity(this, 2, new Intent(), 0);
        Intent buttonIntent = new Intent(this, CancelNotification.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 3, buttonIntent, 0);
        return new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setContentTitle(title)
                .setContentText(description)
                .setCategory(NotificationCompat.CATEGORY_CALL)
                .setContentIntent(mainIntentPending)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(false)
                .setOngoing(true)
                .addAction(0, "Cancel notification", pendingIntent)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.icon);
    }

//接收

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("settings_prefs", Context.MODE_PRIVATE);
    boolean isSoundDisabled= sharedPreferences.getBoolean("enableSound", false);
    sharedPreferences.edit().putInt("notificationId", 1).apply();
    notificationHelper = new NotificationHelper(context, "Channel description", isSoundDisabled, "NotifID", "ChannelName");
    nb = notificationHelper.getChannelNotificationFajr("Notification Name", "Notification description", "NotifID");
    Uri uri = null;
    if (!isSoundDisabled)
        uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notificationsound);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        nb.setSound(uri);
    }

    notificationHelper.getManager().notify(1, nb.build());
}

标签: javaandroidandroid-studiopush-notificationnotifications

解决方案


推荐阅读