簡體   English   中英

在 Android O 上禁用通知聲音

[英]Disable notification sound on Android O

我嘗試在下面的方法中擺脫通知聲音

我能夠將它減少到只關閉一次,但它應該在 Android O和更低版本中完全靜音

我在 stackoverflow 和 google 上搜索了很長時間,但直到現在都沒有完全奏效。

任何幫助表示贊賞。

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel
                    (NOTIFICATION_CHANNEL_ID, "Test Notifications", NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setSound(null, null);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel test");
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color))
                .setOnlyAlertOnce(true);

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
 }

經過更多的研究,我想通了。

這是至關重要的部分:

//Configure the notification channel, NO SOUND
notificationChannel.setDescription("no sound");
notificationChannel.setSound(null,null); <---- ignore sound
notificationChannel.enableLights(false);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);

起初在實現這一點時,它仍然無法正常工作,但在卸載我的應用程序並重新安裝后一切正常。

如果有人需要它,這是正確的代碼:

public void showUpdateProgressNotification(int id, String appName, int progress, String status, long downloadStart) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "My app no sound", NotificationManager.IMPORTANCE_LOW
            );

            //Configure the notification channel, NO SOUND
            notificationChannel.setDescription("no sound");
            notificationChannel.setSound(null,null);
            notificationChannel.enableLights(false);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.enableVibration(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        Intent cancelIntent = new Intent(ACTION_FILE_CANCEL);
        cancelIntent.putExtra("id", id);
        PendingIntent cancel = PendingIntent.getBroadcast(this, ACTION_FILE_CANCEL.hashCode() + id,
                cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(appName)
                .setContentText(status)
                .setSmallIcon(android.R.drawable.stat_sys_download)
                .setDefaults(0)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication_.getInstance().getResources(), R.mipmap.ic_launcher))
                .setProgress(100, progress, progress == 0)
                .setWhen(downloadStart)
                .setContentIntent(cancel)
                .setGroup(GROUP_KEY)
                .addAction(R.drawable.ic_close_black_24dp, "Cancel", cancel)
                .setColor(MyApplication_.getInstance().getResources().getColor(R.color.apps_color));

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
        inboxStyle.addLine(status);
        notification.setStyle(inboxStyle);

        notificationManager.notify(id, notification.build());

        addNotification(id);
    }

無論頻道設置如何,您都可以使用以下方法確保個別通知不發出聲音:

NotificationCompat.Builder.setSilent(true)

無論通知渠道設置如何,這都有效。 這允許您擁有一個默認發出聲音的頻道,但允許您根據需要發布靜音通知,而無需使整個頻道靜音。

參考: https : //developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSilent(boolean)

只需添加

NotificationCompat.Builder.setNotificationSilent()

這適用於所有 Android 版本,在所有情況下,即使您設置了 PRIORITY_HIGH

確保卸載應用程序並重新安裝

示例:

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentText("text")
                .setContentTitle("content")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setNotificationSilent() // just keep this line
                .setSmallIcon(R.drawable.logo)
                .setTicker("text"))

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder.setChannelId(CHANNEL_ID)
            }

一個更簡單的方法是給你的通知渠道低優先級:

    val chan = NotificationChannel(
        channelId,
        channelName, NotificationManager.IMPORTANCE_LOW
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM