Android Notification 消息通知 相關資料.md


Android Notification 消息通知 相關資料

Android 5.0 Lollipop (API 21)無法正常顯示通知圖標,只能看到一個白色方塊或灰色方塊的問題

解決方案

詳情見參考資料2.

  1. 使用在線PS 去掉原來圖標的背景色,保留主題圖標為白色
    Photopea | Online Photo Editor
  2. 打開 Android Asset Studio Notification icon generator 然后上傳新LOGO
  3. 下載 處理好后的新圖標即可.
    android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.mipmap.ic_launcher_white : R.mipmap.ic_launcher

參考資料

  1. Android 5.0 行為變更 | Android Developers

    在白色(或非常淺)的背景上使用深色文本繪制通知,以便與新的 Material Design 小部件匹配。請確保您的所有通知都與新的配色方案協調一致。如果您的通知看上去不協調,請進行修正:

    • 使用 setColor() 在您的圖標圖像后面的圓形中設置重點色彩。
    • 更新或移除使用色彩的資源。系統在操作圖標和主要通知圖標中忽略所有非阿爾法通道。您應假設這些圖標僅支持阿爾法通道。系統用白色繪制通知圖標,用深灰色繪制操作圖標。
  2. Android Push Notifications: Icon not displaying in notification, white square shown instead - Stack Overflow

    • For 5.0 Lollipop "Notification icons must be entirely white".
    Notification notification = new NotificationCompat.Builder(this);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notification.setSmallIcon(R.drawable.icon_transperent);
       notification.setColor(getResources().getColor(R.color.notification_color));
    } else {
        notification.setSmallIcon(R.drawable.icon);
    }
    
    • I found a link where we can generate our own white icon,
      try this link to generate white icon of your launcher icon.
      Open this Link Android Asset Studio and upload your ic_launcher or notification icon
  3. android 5.0以上通知欄、狀態欄圖標變成白色 - 暈菜一員 - 博客園

    因為google在android5.0上面做了限制,為了統一系統風格。之后的狀態欄icon就不能夠隨便用一張色彩豐富的圖片了,只能夠有白色和透明兩個顏色出現。

    5.0以上(不包含5.0),系統默認通知欄圖標為系統啟動圖標,會自動將通知欄的圖標(有色區域)全部填充為白色,

  4. Android 的 notification 圖標問題 - V2EX

    因為原先的那種形式的圖標不好看, 顏色太花, 在狀態欄上一點也不好看.
    並且這也是 Material Design 的規范了 ( https://www.google.com/design/spec/patterns/notifications.html) 實際上這樣的改變在 Android 5.0 里好看了很多,
    如果你看到有些廠商還不知道的, 那么就是他們菜或者是上班混日子.

Android 8.0 Oreo(API 26) 及更高版本 如何更新 消息通知渠道 設置的問題

解決方案:無!

谷歌官方文檔說明了,一旦渠道創建完畢,那么最終管理權利就移交給用戶了. 程序最多可以將其刪除, 想修改是不可能的了.

ByYe:間接解決方案=根據title或者sound的不同,創建不同的渠道.來間接實現.

問題詳情

想重用一個渠道, 只是修改不同的部分,比如提示聲音鈴聲不同.

參考資料

  1. https://developer.android.com/guide/topics/ui/notifiers/notifications#limits

    發布限制
    從 Android 8.1(API 級別 27)開始,應用無法每秒發出一次以上的通知提示音。如果應用在一秒內發出了多條通知,這些通知都會按預期顯示,但是每秒中只有第一條通知發出提示音。

    不過,Android 還對通知更新頻率設定了限制。如果您過於頻繁地發布有關某條通知的更新(不到一秒內發布多個),系統可能會放棄部分更新。

  2. How to properly update notification channel android oreo - Stack Overflow

    Once a notification channel is created, the user has ultimate control over it's settings.

    As the developer, you can only change the channel's title and description.

    If you want to recreate the channel, you have to use a different id.

    See: https://developer.android.com/training/notify-user/channels

  3. Create and Manage Notification Channels | Android Developers

    • Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel
    • After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. Though you can still change a channel's name and description.
    • But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active.
  4. Android Oreo 通知新特性,這坑老夫先踩了 - 簡書

    其中“lockscreenVisibility”和“setBypassDnd”是無法生效的,因為從源碼中來看,

    只能被系統或排序服務(Android Notification Ranking Service)更改。

Android 7.0 Nougat(API 24) 及更高版本 消息通知 設置的自定義鈴聲 無法正常播放問題

解決方案

  1. 詳情參見 參考資料1
  2. 采用的是使用自定義ContentProvider The Axe: Use a Custom ContentProvider
  3. 實際測試效果: Android 8.1 + Android 4.4.4 版本都正常.

解決步驟:

  1. 下載最新版本的cwac-provider-0.5.3.jar並集成到項目中

    GitHub - commonsguy/cwac-provider: CWAC-Provider: Helping to Make Content Providers Sane

  2. 按文檔要求在清單文件里AndroidManifest.xml添加了以下代碼:

    <provider
              android:name="com.commonsware.cwac.provider.StreamProvider"
              android:authorities="${applicationId}.DataFilesSoundsStreamProvider"
              android:exported="true">
        <meta-data
                   android:name="com.commonsware.cwac.provider.STREAM_PROVIDER_PATHS"
                   android:resource="@xml/data_files_sounds_paths" />
    </provider>
    
  3. 在項目資源目錄里添加了res/xml/data_files_sounds_paths.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <files-path
                name="data_files_sounds"
                path="sounds"
                readOnly="true" />
    </paths>
    
  4. 將代碼中原使用Uri.fromFile(file)的地方替換為com.commonsware.cwac.provider.StreamProvider.getUriForFile(mContext.getPackageName() + ".DataFilesSoundsStreamProvider", file)即可

問題詳情

問題還原步驟:

  1. 使用 NotificationCompat.Builder.setSound 設置自定義鈴聲
  2. 自定義鈴聲 位於 SDCard外置存儲卡Environment.getExternalStorageDirectory() 某個目錄里.

結果:

  1. Android 6 之前的版本 正常 播放.

  2. Android 7 以后的版本 無法 播放.

    拋出異常:StrictMode: file:// Uri exposed through Notification.sound

參考資料

  1. java - Android 7.0 Notification Sound from File Provider Uri not playing - Stack Overflow

    If you were using file: Uri values, they no longer work on Android 7.0 if your targetSdkVersionis 24 or higher, as the sound Uri is checked for compliance with the ban on file: Uri values.
    假如你還在使用file:uri形式的路徑值, 那么Android 7.0以后的版本就不再支持了(只要targetSdkVersion > 24)

    However, if you try a content: Uri from, say, FileProvider, your sound will not be played... because Android does not have read access to that content.

    但是,假如你嘗試使用content: 開頭的Uri對象,例如通過FileProvider可以拿到這類對象,結果你的自定義通知鈴聲還是不能正常播放囧。原因是Android系統本身沒有你指定的文件的讀取權限

    解決方案有:

    1. 使用grantUriPermissions()授權,讓其具有讀取權限The Scalpel: grantUriPermissions()

      grantUriPermission("com.android.systemui", soundUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
      缺點: 在於你不知道給授權,Android默認會是com.android.systemui,至於其它廠商的系統是不是這個,就很難說了.

      Thanks a lot for the thorough answer. To verify the robustness of the grantUriPermission solution ('The Scalpel'), I've tested it in different physical devices (including LG, Motorola, HTC, Samsung and Sony), from API 15 up to API 24. It works correctly on all of them, so the solution seems pretty solid (although certainly hacky). – jmart Sep 7 '16 at 18:24

    2. 不提供完全自定義的鈴聲,僅僅提供部分可選擇的鈴聲 The Guillotine: No More User Files

      原因是:android.resource開頭的Uri對象路徑Android系統默認是支持直接讀取的,所以只要將部分可供選擇的鈴聲放在APP的RAW資源目錄里即可.

      缺點:在於讓用戶無法完全自定義自己的鈴聲了,可能你的用戶不一定會買賬.

    3. 使用自定義ContentProvider The Axe: Use a Custom ContentProvider

      FileProvider 不能直接用,因為一旦設置成 exported 那么一啟動APP就會崩潰.

      那么可用的方案就是自定義一個支持exported且具有讀取權限的 ContentProvider了.

      可以參見: GitHub - commonsguy/cwac-provider: CWAC-Provider: Helping to Make Content Providers Sane

      Using a read-only ContentProvider (see "The Axe") is the better answer. I'll probably rig up my StreamProvider such that if you mark it as exported, it treats everything as read-only, which would handle this problem fairly cleanly. – CommonsWare


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM