簡體   English   中英

android oreo上的通知 - 動態啟用聲音/振動

[英]Notifications on android oreo - dynamically enable sound/vibration

我有一個前台服務,顯示進度通知,如果完成,通知將重新用作正常通知,以顯示服務的結果

我允許用戶在我的應用程序內部定義最終通知是否為靜音。 所以這就是我的所作所為:

// 1) Create notifcation channel in my app, once only
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);

// 2) Init notification
notificationBuilder = new NotificationCompat.Builder(this, notificationChannelId);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setOngoing(true);
notificationBuilder.setTicker(ticker);
notificationBuilder.setContentText(title);

// 3) Updating the notification to show the services progress
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(subTitle);

// 4) preparing the final notification
if (!MainApp.getPrefs().silentNotifications()) {
    if (globalError || updated > 0 || prepared > 0 || contactsWithError.size() > 0) {
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        notificationBuilder.setVibrate(new long[]{0, 500});
        notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    } else {
        notificationBuilder.setVibrate(new long[]{});
        notificationBuilder.setSound(null);
    }
}

問題

  • 一個android oreo用戶告訴我,通知是在每次更新時發出聲音/振動
  • 同一個用戶告訴我,重新啟動后,聲音/振動消失了

我想要的是

  • 我希望通知通道默認振動並播放聲音(如果用戶沒有在我的頻道的android設置中更改此聲明)
  • 即使啟用了聲音/振動,我也想動態地決定不播放聲音/振動
  • 如果用戶禁用我的頻道的聲音/振動,我很好,因為我不會播放聲音,也不會隨時振動

我該怎么做到這一點?

編輯 - 一個解決方案如下:

使用兩個渠道:

  • 一個用於更新通知
  • 一個用於最終結果通知。

這對我來說很奇怪,我會假設我可以使用一個頻道,默認設置聲音並使用此頻道。 如果用戶允許我播放聲音,我應該能夠定義自己播放聲音(似乎我現在不得不播放聲音)。 否則我當然不會播放聲音。 看起來目前您需要為每項服務提供兩個頻道,這些頻道會顯示通知中的進度,並在完成后默認播放聲音。 向用戶解釋是不好的,因為你需要兩個通道用於同一個動作,一個用於進度,另一個用於最終結果......

我對通知的聲音感到惱火。 我這樣做了

.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.silence))

還有這個

.setOnlyAlertOnce(true)

另請注意,您還必須在頻道中設置它們。

暫無
暫無

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

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