簡體   English   中英

Android 推送通知:通知中未顯示圖標,而是顯示白色方塊

[英]Android Push Notifications: Icon not displaying in notification, white square shown instead

我的應用程序生成通知,但我為該通知設置的圖標沒有顯示。 相反,我得到一個白色方塊。

我嘗試調整圖標的 png 大小(尺寸 720x720、66x66、44x44、22x22)。 奇怪的是,當使用較小的尺寸時,白色方塊會更小。

我已經用谷歌搜索了這個問題,以及生成通知的正確方法,從我讀過的內容來看,我的代碼應該是正確的。 可悲的是,事情並非如此。

我的手機是帶有 Android 5.1.1 的 Nexus 5。 模擬器上也存在問題,三星 Galaxy s4 的 Android 5.0.1 和摩托羅拉 Moto G 的 Android 5.0.1(我都借了,現在沒有)

通知代碼如下,以及兩個屏幕截圖。 如果您需要更多信息,請隨時詢問。

謝謝你們。

@SuppressLint("NewApi") private void sendNotification(String msg, String title, String link, Bundle bundle) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    resultIntent.putExtras(bundle);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification notification;
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound);
    notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.lg_logo)
                .setContentTitle(title)
                .setStyle(new Notification.BigTextStyle().bigText(msg))
                .setAutoCancel(true)
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .setSound(sound)
                .build();
    notificationManager.notify(0, notification);
}

不打開通知打開的通知

原因:對於 5.0 Lollipop,“通知圖標必須全白”。

如果我們通過將 target SDK 設置為 20 來解決白色圖標問題,我們的應用將不會以 Android Lollipop 為目標,這意味着我們無法使用 Lollipop 特定的功能。

目標 SDK 21 的解決方案

如果要支持 Lollipop Material Icons,請為 Lollipop 及以上版本制作透明圖標。 請參考以下內容: https ://design.google.com/icons/

請查看http://developer.android.com/design/style/iconography.html ,我們將看到白色樣式是通知在 Android Lollipop 中的顯示方式。

在 Lollipop 中,Google 還建議我們使用一種將顯示在白色通知圖標后面的顏色。 參考鏈接: https ://developer.android.com/about/versions/android-5.0-changes.html

無論我們想在哪里添加顏色https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)

低於和高於 Lollipop OS 版本的 Notification Builder 的實現將是:

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);
} 

注意: setColor 僅在 Lollipop 中可用,它只影響圖標的背景。

徹底解決你的問題!!

如果您使用的是 Google Cloud Messaging,則僅通過更改圖標無法解決此問題。 例如,這將不起作用:

 Notification notification  = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent)
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true)
                .build();

即使ic_notification 是透明的和白色的。 它還必須在 Manifest 元數據中定義,如下所示:

  <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

            android:resource="@drawable/ic_notification" />

元數據位於application標簽下,以供參考。

我真的建議遵循Google 的設計指南

上面寫着“通知圖標必須全白”。

在 Android Manifest 中聲明此代碼:

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" 

android:resource="@drawable/ic_stat_name" />

(Android Studio 3.5)如果您使用的是最新版本的 Android Studio,您可以生成通知圖像。 右鍵單擊您的 res 文件夾> New > Image Asset 然后,您將看到配置圖像資產,如下圖所示。 圖標類型更改為通知圖標 您的圖像必須是白色和透明的。 配置圖像資產將強制執行該規則。 配置圖像資產 重要提示:如果您希望圖標用於雲/推送通知,則必須在應用程序標簽下添加元數據才能使用新創建的通知圖標。

  <application>
      ...
      <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
          android:resource="@drawable/ic_notification" />
 <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

        android:resource="@drawable/ic_notification" />

在應用程序塊的 manifest.xml 文件中添加這一行。

嘗試這個

我遇到了同樣的問題,我嘗試了很多答案,但沒有得到任何解決方案,最后我找到了解決問題的方法。

- 制作具有透明背景的通知圖標。應用程序的寬度和高度必須如下所示,並將所有這些粘貼到您的項目->應用程序->src->main->res

  • MDPI 24*24

  • HDPI 36*36

  • XHDPI 48*48

  • XXHDPI 72*72


在上面粘貼下面這一行之后你的 onMessageReceived 方法


Intent intent = new Intent(this, News.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                notificationBuilder.setSmallIcon(R.drawable.notify)
                                      //            .setContentTitle(title)
                            //                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
            } else
                {
                    notificationBuilder.setSmallIcon(R.drawable.notify)
                       //                                .setContentTitle(title)
                        //                        .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            }
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());

不要忘記在清單文件中添加此代碼

<meta-data 
android:name="com.google.firebase.messaging.default_notification_icon" 
android:resource="@drawable/app_icon" />

我們可以這樣做:

創建通知構建器的新對象並使用通知構建器對象調用setSmallIcon() ,如下面的代碼所示。

創建一個方法,我們將在其中檢查我們正在安裝我們的應用程序的操作系統版本。 如果它低於 Lolipop 即 API 21,那么它將采用帶有背景顏色的普通應用程序圖標,否則它將采用沒有任何背景的透明應用程序圖標。 因此,使用 os version >= 21 的設備將使用 Notification builder 類的setColor()方法設置圖標的背景顏色。

示例代碼:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));

private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             int color = 0x008000;
             notificationBuilder.setColor(color);
             return R.drawable.app_icon_lolipop_above;

    } 
    return R.drawable.app_icon_lolipop_below;
}

我已經通過將以下代碼添加到清單來解決問題,

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_name" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/black" />

其中ic_stat_name在 Android Studio 上創建 右鍵單擊​​ res >> New >>Image Assets >> IconType(Notification)

我必須在服務器 php 端使用通知有效負載再做一步

$message = [
    "message" => [
        "notification" => [
            "body"  => $title , 
            "title" => $message
        ],

        "token" => $token,

    "android" => [
           "notification" => [
            "sound"  => "default",
            "icon"  => "ic_stat_name"
            ]
        ],

       "data" => [
            "title" => $title,
            "message" => $message
         ]


    ]
];

注意部分

    "android" => [
           "notification" => [
            "sound"  => "default",
            "icon"  => "ic_stat_name"
            ]
        ]

其中圖標名稱為"icon" => "ic_stat_name"應該與清單上的設置相同。

如果您想提供棒棒糖支持通知圖標,請制作兩種類型的通知圖標:

  1. 正常通知圖標:適用於以下棒棒糖版本。
  2. 帶有透明背景的通知圖標:棒棒糖及以上版本。

現在根據操作系統版本在運行時為通知生成器設置適當的圖標:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    mBuilder.setSmallIcon(R.drawable.ic_push_notification_transperent);
} else {
    mBuilder.setSmallIcon(R.drawable.ic_push_notification);
}

最后我得到了這個問題的解決方案。

僅當應用程序根本未運行時才會出現此問題。 (既不在后台也不在前台) 應用程序在前台或后台運行時,通知圖標會正確顯示。(不是白色方塊)

所以我們要設置的是后端API中通知圖標的配置與前端相同。

在前端,我們使用了React Native ,對於推送通知,我們使用了react-native-fcm npm 包

FCM.on("notification", notif => {
   FCM.presentLocalNotification({
       body: notif.fcm.body,
       title: notif.fcm.title,
       big_text: notif.fcm.body,
       priority: "high",
       large_icon: "notification_icon", // notification icon
       icon: "notification_icon",
       show_in_foreground: true,
       color: '#8bc34b',
       vibrate: 300,
       lights: true,
       status: notif.status
   });
});

我們使用使用Node.jsfcm-push npm 包作為推送通知的后端,並將有效負載結構設置如下。

{
  to: '/topics/user', // required
  data: {
    id:212,
    message: 'test message',
    title: 'test title'
  },
  notification: {
    title: 'test title',
    body: 'test message',
    icon : 'notification_icon', // same name as mentioned in the front end
    color : '#8bc34b',
    click_action : "BROADCAST"
  }
}

它基本上搜索存儲在我們的Android系統中本地的notification_icon圖像。

通知是灰度的,如下所述。 盡管其他人已經寫過,但它們並不是非黑即白的。 您可能已經看到具有多種陰影的圖標,例如網絡強度條。

在 API 21 (Lollipop 5.0) 之前,彩色圖標有效。 您可以強制您的應用程序以 API 20 為目標,但這會限制您的應用程序可用的功能,因此不建議這樣做。 您可以測試正在運行的 API 級別並適當地設置彩色圖標或灰度圖標,但這可能不值得。 在大多數情況下,最好使用灰度圖標。

圖像有四個通道,RGBA(紅/綠/藍/alpha)。 對於通知圖標,Android 會忽略 R、G 和 B 通道。 唯一重要的通道是 Alpha,也稱為不透明度。 使用可讓您控制繪圖顏色的 Alpha 值的編輯器設計您的圖標。

Alpha 值如何生成灰度圖像:

  • Alpha = 0(透明)- 這些像素是透明的,顯示背景顏色。
  • Alpha = 255(不透明)——這些像素是白色的。
  • Alpha = 1 ... 254 — 這些像素正是您所期望的,提供透明和白色之間的陰影。

setColor改變它:

  • 調用NotificationCompat.Builder.setColor(int argb) Notification.color的文檔中:

    標准樣式模板在呈現此通知時應用的強調色(一個 ARGB 整數,如 Color 中的常量)。 當前的模板設計通過在此顏色的字段頂部覆蓋圖標圖像(以白色印刷)來構建彩色標題圖像。 Alpha 分量被忽略。

    我對 setColor 的測試表明 Alpha 組件未被忽略。 較高的 Alpha 值會將像素變為白色。 較低的 Alpha 值將像素轉換為通知區域中的背景顏色(我的設備上為黑色),或下拉通知中的指定顏色。

解決此問題的要求:

  1. 圖像格式:32 位 PNG(帶 alpha)

  2. 圖像應該是透明的

  3. 透明色指數:白色 (FFFFFF)

來源:http: //gr1350.blogspot.com/2017/01/problem-with-setsmallicon.html

我找到了一個鏈接,我們可以在其中生成我們自己的白色圖標,

嘗試此鏈接以生成啟動器圖標的白色圖標。

打開此鏈接並上傳您的 ic_launcher 或通知圖標

對於自定義的本地通知,在 AndroidManifest.xml 添加以下元數據然后它將起作用。

 <application
    android:name="xxxxxx"
        android:label="xxxxxx"
        android:icon="@mipmap/ic_launcher"
        
        >

       <meta-data
                android:name="your_apps_bundle_id.default_notification_icon"
                android:resource="@drawable/ic_notif" />

......

我在 android 8.0 上有類似的問題。 嘗試使用白色圖標資源。 當我嘗試使用彩色圖像作為圖標時,我有白色方塊,當我將它替換為白色圖標時,它就開始工作了。

您可以為不同的版本使用不同的圖標。 只需在您的圖標上設置邏輯,如下所示:

int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.colored_: R.drawable.white_tint_icon_for_lolipop_or_upper;

對於 SDK >= 23,請添加 setLargeIcon

notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(context.getResources(), R.drawable.lg_logo))
            .setContentTitle(title)
            .setStyle(new Notification.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setContentText(msg)
            .setContentIntent(contentIntent)
            .setSound(sound)
            .build();

當您想要保留彩色圖標時 - 解決方法
將顏色略有不同的像素添加到圖標中。
在我的情況下,一個帶有陰影和光線的黑色圖標。 當添加深藍色像素時,它可以工作。

要減少 SDK 特定版本,您可以簡單地執行以下操作:(將 '#' 替換為 '0x')

Notification notification = new NotificationCompat.Builder(this);
notification.setSmallIcon(R.drawable.icon_transperent);
notification.setColor(0x169AB9); //for color: #169AB9

我只是將我的png轉換為透明的png,然后圖標與圖片的形狀相同,但顏色不同

使用任何網站刪除圖標的背景,建議一個是https://www.remove.bg/然后只需在下載后使用該圖像即可解決您的問題。

暫無
暫無

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

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