Firebase云消息传递:如何在安卓上设置通知图标?

13
我在安卓工作室设置通知图标时遇到了问题。
我按以下方式设置了可绘制的文件夹:

输入图像描述

同时,我还在我的AndroidManifest.xml文件中设置了默认图标:

  <meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/notification_icon" />
在这里,我将icon字段设置为notification_iconhttps://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json (附言:我知道这是GCM,但它可以工作。除了图标之外,我收到了所有通知)。
我错过了什么?我只看到一个灰色圆圈内部的白色正方形。
这是我的后端代码:Pushex.push(%{title:user.name,body:“message goes here”,badge:1,sound:“default”,icon:“notification_icon”},to:user.fcm_token,using::gcm)https://github.com/tuvistavie/pushex

我应该把那行代码放在哪里? - bigpotato
你能包含你的样例载荷(漂亮的打印输出)吗?你提到你使用了 notification_icon,但在文档中应该只用 icon。或者这只是你在代码里设置的方式? - AL.
@AL 已更新,包含我的后端调用。 - bigpotato
@Edmund,请问你的AndroidManifest文件中<meta-data ../>部分在哪里? 它应该在以下结构中。 <application> <meta-data .... /> ... </application> - Mann
@Mann 终于在数小时后找到了答案...请看下面的回答。 - bigpotato
你好,能否解释一下你的drawable文件夹,因为你的照片链接已经失效了。 - Luke Pighetti
4个回答

5

我正在使用FCM插件,以下是在2019年9月适用的方法:

  1. config.xml文件(yourapp/config.xml)中,在末尾的<widget>标签中添加以下内容:xmlns:android="http://schemas.android.com/apk/res/android"

现在它应该看起来像这样:

<widget id="com.mydomain.app" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android">

或者,只需复制上面的行,并使用自己的值替换widget id。

  1. 在同一个config.xml文件中: 在对应于<platform name="android">的标签之前,添加以下内容:
<config-file parent="/manifest/application/" target="app/src/main/AndroidManifest.xml">
            <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/fcm_push_icon" />
</config-file>
  1. 访问以下链接: 通知图标生成器

上传白色版本(单色)的透明背景下的标志。如果上传彩色版本,将会得到一个深灰色的图标,看起来很难看。如果没有白色版本的标志,请设计一个。其余的设置保持不变。对于名称文本框中的值,请输入:fcm_push_icon。然后点击蓝色的圆形按钮下载zip文件。

  1. 解压刚刚在上一步下载的zip文件并提取其内容到一个文件夹中。您会注意到它包含一个res文件夹。如果打开此文件夹,它将包含其他文件夹,其名称如下:

    • drawable-hdpi
    • drawable-mdpi
    • drawable-xhdpi
    • drawable-xxhdpi
    • drawable-xxxhdpi

每个文件夹都将包含一个名为"fcm_push_icon.png"的PNG图标。这些不同文件夹中图标的唯一区别是它们的大小。

  1. 在您的应用程序中打开以下文件路径(如果不存在,请按照下面所示创建它):

yourApp/platforms/android/app/src/main/res

只需将上述第4点中列出的5个文件夹全部复制到上面显示的位置,即将其复制到res文件夹中,即复制到yourApp/platforms/android/app/src/main/res中。

就这样!现在只需构建您的应用程序。每当收到通知时,您将在通知中看到您的应用程序图标(至少在Android上是这样的)。

如果有人已经找到如何在Android通知中显示彩色图标的解决方案,请分享您的解决方案。


@un.spike 谢谢您的确认。非常感激。很高兴听到它对您有用 :) - Devner

4

问题已经解决了...这很愚蠢。我受到下面回答的启发: https://dev59.com/wl4c5IYBdhLWcg3wCGf2#28387744

我用我的应用图标作为通知图标:

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

接下来,在android/app/build.gradle文件中加入以下代码:

defaultConfig {
    targetSdkVersion 20 // this has to be < 21
    ...
}

希望我能为他人节省数小时的时间


12
虽然这种方法可以奏效,但其实你不需要回到20级。如果你想使用21级及以上的版本(这是常见的),你需要创建一个白色背景透明的通知图标才能生效。你的启动器图标不应被重复使用,因为它是全彩色的,会显示成一个白色方块。 - Kevin
3
这是一种对现代应用程序有害的建议。不要将目标SDK降级。 - CoolMind

3

看一下这段代码,它可能对你有所帮助。

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
            private static final String TAG = "FCM Service";

            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {       
                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            Intent intent = new Intent(getApplicationContext(), YourClass.class);
                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), NotificationID.getID(), intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(getNotificationIcon())
                            .setContentTitle(remoteMessage.getData().get("title"))
                            .setContentText(remoteMessage.getData().get("body"))
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get("body")))
                            .setContentIntent(contentIntent);
                    NotificationManager notificationManager =
                            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(NotificationID.getID(), notificationBuilder.build());
            }
            private int getNotificationIcon() {
                boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
                //If the build version is higher than kitkat we need to create Silhouette icon. 
                return useWhiteIcon ? R.mipmap.ic_notification : R.mipmap.ic_launcher;
            }

            public static class NotificationID {
                private static final AtomicInteger c = new AtomicInteger(0);

                public static int getID() {
                    return c.incrementAndGet();
                }
            }
        }

如果构建版本高于kitkat,我们需要创建Silhouette图标。为此,有一个在线工具可用。请参考:https://romannurik.github.io/AndroidAssetStudio/icons-notification.html

3

然而,使用SDK 9.8.0版本,你可以自定义默认设置!在你的AndroidManifest.xml文件中,你可以设置以下字段来自定义图标和颜色:

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

并且

<manifest>
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">

    <!-- Add your meta-data here-->

    <activity 
        android:name=".MainActivity" 
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>


2
我已经有了 (它在我的问题中): <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/notification_icon" /> - bigpotato
Suggestion: add 'tools:replace="android:resource"' - Oliver Dixon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接