Firebase云消息:如何在android上设置通知图标?
我在android studio上设置通知图标时遇到问题 我设置了可绘制文件夹,如下所示: 我还在AndroidManifest.xml文件中设置了默认图标:Firebase云消息:如何在android上设置通知图标?,android,firebase,react-native,firebase-cloud-messaging,Android,Firebase,React Native,Firebase Cloud Messaging,我在android studio上设置通知图标时遇到问题 我设置了可绘制文件夹,如下所示: 我还在AndroidManifest.xml文件中设置了默认图标: 在这里,我将图标字段设置为notification\u icon:(注意,我知道这是GCM,但它可以工作。除了图标之外,我还收到了通知) 我错过了什么?我看到的只是灰色圆圈里的一个白色正方形 这是我的后端代码:Pushex.push(%%{title:user.name,body:“message goes here”,徽章:1,声
在这里,我将图标字段设置为notification\u icon
:(注意,我知道这是GCM,但它可以工作。除了图标之外,我还收到了通知)
我错过了什么?我看到的只是灰色圆圈里的一个白色正方形
这是我的后端代码:
Pushex.push(%%{title:user.name,body:“message goes here”,徽章:1,声音:“default”,图标:“notification_icon”},to:user.fcm_token,using::gcm)
()检查此代码。这可能对你有帮助
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,我们需要创建轮廓图标。对于这一点,在线工具是可用的
请参阅:使用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" />
及
修复了它。。。这太愚蠢了。我的灵感来自于这个答案: 我将我的通知图标设置为我的应用程序图标:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
希望我能为其他人节省几个小时的生命我正在使用,以下是对我有效的方法(2019年9月):
xmlns:android=”http://schemas.android.com/apk/res/android“
或者简单地复制上面的行,用您自己的替换widget id的值
- 可拉伸hdpi
- 可拉伸mdpi
- 可拉伸xhdpi
- 可拉伸xxhdpi
- 可拉伸xxxhdpi
yourApp/platforms/android/app/src/main/res
只需将上面第4点中列出的所有5个文件夹复制到上面显示的位置,即进入res文件夹,即进入yourApp/platforms/android/app/src/main/res
就这样!现在只需构建你的应用程序。无论何时收到通知,您都会在通知中看到您的应用程序图标(至少在Android上)
如果有人知道如何在Android通知中显示彩色图标,请分享您的解决方案。我将把这一行放在哪里?您能提供您的示例有效负载(打印精美)吗?您提到您使用的是
通知图标
,但在文档中它应该是图标
。还是只是你在代码中设置的方式?@AL用我的后端更新了call@EdmundAndroidManifest文件中的部件在哪里?它应该在这个层次结构中@曼恩在几个小时后才发现。。。看看下面我的答案,我已经有了(这在我的问题中):建议:添加'tools:replace=“android:resource”
,这样做有效时,你真的不需要回到20级。如果您想使用21及以上,这是常见的,您需要创建一个通知图标,该图标是白色的,背景透明。您的启动器图标不应重复使用,因为它是全色的,将显示为白色正方形。这对现代应用程序是一个有害的建议。不要降级target SDK.@un.spike谢谢您的确认。谢谢。很高兴听到它对您有用:)
<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>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />
defaultConfig {
targetSdkVersion 20 // this has to be < 21
...
}
<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>