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

[TOC]html

Android Notification 消息通知 相關資料

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

解決方案

詳情見參考資料2.java

  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 Developersandroid

    在白色(或很是淺)的背景上使用深色文本繪製通知,以便與新的 Material Design 小部件匹配。請確保您的全部通知都與新的配色方案協調一致。若是您的通知看上去不協調,請進行修正:git

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

    • 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以上通知欄、狀態欄圖標變成白色 - 暈菜一員 - 博客園app

    由於google在android5.0上面作了限制,爲了統一系統風格。以後的狀態欄icon就不可以隨便用一張色彩豐富的圖片了,只可以有白色和透明兩個顏色出現。ide

    5.0以上(不包含5.0),系統默認通知欄圖標爲系統啓動圖標,會自動將通知欄的圖標(有色區域)所有填充爲白色,測試

  4. Android 的 notification 圖標問題 - V2EXui

    由於原先的那種形式的圖標很差看, 顏色太花, 在狀態欄上一點也很差看. 而且這也是 Material Design 的規範了 ( https://www.google.com/design/spec/patterns/notifications.html) 實際上這樣的改變在 Android 5.0 裏好看了不少, 若是你看到有些廠商還不知道的, 那麼就是他們菜或者是上班混日子.this

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

相關文章
相關標籤/搜索