Java NotificationCompat声音不响

时间:2018-12-28 04:59:45

标签: java android notifications

我正在为我的应用程序创建通知。通知类型为进度条时,我不想发出声音。

// Run method 
progressNotification(context);

public void progressNotification(Context context){
    // Simulation download progress
    int min = 0;
    int max = 100;
    while (min<=max){
        try{
            showNotification(context, true, min, max);
            Thread.sleep(100);
        }catch(InterruptedException ex){
        }
        min++;
    }
    // Send notification with sound
    showNotification(context, false, 0, 0);
}

public void showNotification(Context context, Boolean progress, int min, int max){
    Intent intent = new Intent(context, EntryActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    String notificationChannelId = createNotificationChannel(context, progress ? false : true);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, notificationChannelId);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(context.getString(R.string.message_title))
            .setContentText(context.getString(R.string.message_content))
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(context.getString(R.string.mask_message_content)))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    if(progress) {
        if(min<max) {
            mBuilder.setContentText("Downloading...").setProgress(max, min, false);
        }else{
            mBuilder.setContentText("Download Complete!").setProgress(0, 0,false);
        }
    }

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(234, mBuilder.build());
}

private String createNotificationChannel(Context context, Boolean audio) {
    String channelId = "channelId";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "channel_name";
        String description = "channel_description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, name, importance);
        channel.setDescription(description);

        // Here the condition
        if(!audio) channel.setSound(null, null);

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        return channelId;
    }
    return null;
}

如果我将showNotification()设置为“ true”,则表示禁用声音,它可以工作,但当我再次显示进度为“ false”的通知时,则表示我启用了声音,但仍处于禁用状态。

3 个答案:

答案 0 :(得分:0)

您也可以这样做,我相信这将根据您实现的条件创建新的通知:

               if (disableSound) {
                    mBuilder = new NotificationCompat.Builder(context, "")

                            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                            .setSound(uri)
                            .setContentTitle("Expiry Date Reminder")
                            .setContentText("Your text")
                            .setAutoCancel(true) // clear notification after click
                            .setContentIntent(pIntent)
                            .setCategory(Notification.CATEGORY_MESSAGE)
                            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                } else {
                    mBuilder = new NotificationCompat.Builder(context, "")
                            .setTicker(model.getProductName())
                            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                            .setSound(uri)
                            .setContentTitle("Expiry Date Reminder")
                            .setContentText("Your text")
                            .setAutoCancel(true) // clear notification after click
                            .setContentIntent(pIntent)
                            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                }

然后其余的代码将在此处:

 Notification notification = mBuilder.build();
 notification.flags = Notification.FLAG_AUTO_CANCEL;

 NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

答案 1 :(得分:0)

您的第二个通知声音不起作用,因为您创建了一个没有声音的通知频道。 (要更新通知频道,您需要删除该频道,然后再次添加。)

// Here the condition
if(!audio) channel.setSound(null, null); 

我建议删除上面的代码行,并在NotificationCompat类中进行以下更改:

if(progress) {
    if(min<max) {
        mBuilder
             //Set priority to PRIORITY_LOW to mute notification sound
            .setPriority(NotificationCompat.PRIORITY_LOW) 
            .setContentText("Downloading...").setProgress(max, min, false);
    }else{
        mBuilder
            .setContentText("Download Complete!").setProgress(0, 0,false);
    }
}

答案 2 :(得分:0)

问题解决了,我必须更新或创建每个请求NotificationChannel的唯一channelId。

private String createNotificationChannel(Context context, Boolean audio) {

    String channelId = "channelId" + Boolean.toString(audio); // Here should be an unique id

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "channel_name";
        String description = "channel_description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(channelId, name, importance);
        channel.setDescription(description);

        // Here the condition
        if(!audio) channel.setSound(null, null);

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        return channelId;
    }
    return null;
}