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

big*_*ato 11 android firebase react-native firebase-cloud-messaging

我在android studio上设置通知图标时遇到问题.

我设置了drawable文件夹,如下所示:

在此输入图像描述

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

  <meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/notification_icon" />
Run Code Online (Sandbox Code Playgroud)

在这里,我将图标字段设置为notification_icon:https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json(ps我知道这是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)

big*_*ato 5

Fixed it... this is so stupid. Was inspired by this answer: /sf/answers/1987142111/

I made my notification icon my app icon:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@mipmap/ic_launcher" />
Run Code Online (Sandbox Code Playgroud)

And then the magic line in android/app/build.gradle...

defaultConfig {
    targetSdkVersion 20 // this has to be < 21
    ...
}
Run Code Online (Sandbox Code Playgroud)

Hope I save hours of someone else's life

  • 虽然这有效,但你真的不需要回到 20 级。如果你想使用 21 级及以上,这是常见的,你需要创建一个白色透明背景的通知图标才能工作。您的启动器图标不应重复使用,因为它是全彩色的,并且只会显示为一个白色方块。 (11认同)
  • 对于现代应用程序来说,这是一个有害的建议。不要降级目标 SDK。 (2认同)

Dev*_*ner 5

我正在使用FCM 插件,以下是对我有用的插件(2019 年 9 月):

  1. config.xml (yourapp/config.xml) 末尾添加以下内容到标签中 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">

或者简单地复制上面的行,将小部件 id 的值替换为您自己的值。

  1. 在同一个config.xml文件中: 在 对应的标签之前,添加以下内容:
<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>
Run Code Online (Sandbox Code Playgroud)
  1. 访问以下链接: 通知图标生成器

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

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

    • 可绘制的 hdpi
    • 可绘制 mdpi
    • 可绘制xhdpi
    • 可绘制-xxhdpi
    • 可绘制-xxxhdpi

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

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

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

只需将上面第 4 点中列出的所有 5 个文件夹复制到上面显示的位置 ie 到res文件夹 ie 到yourApp/platforms/android/app/src/main/res

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

如果有人知道如何让彩色图标出现在 Android 通知中,请分享您的解决方案。


EKN*_*EKN 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();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

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


归档时间:

查看次数:

15081 次

最近记录:

5 年,11 月 前