简体   繁体   English

如何从Firebase获取推送通知上的图标?

[英]How to fetch icon on Push Notification from Firebase?

enter image description here I want to fetch different-different icons on Push Notification from Firebase but here default icon is visible everytime. 在此处输入图像描述,我想从Firebase的“推送通知”中获取不同图标,但是每次都可以看到默认图标。 So what should I do? 所以我该怎么做?

public class Mymessagingservice extends FirebaseMessagingService {

public  void onMessageReceived(RemoteMessage remoteMessage){
    super.onMessageReceived(remoteMessage);
    getimage(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());

}
public void getimage(String title,String message){
    NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"mynotification")
            .setContentTitle(title)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setAutoCancel(true)
            .setContentText(message);


    NotificationManagerCompat manager =NotificationManagerCompat.from(this);
    manager.notify(999,builder.build());

}
}

Make sure that in your AndroidManifest file, you've set the icon for Firebase notification like this: 确保在AndroidManifest文件中,已设置Firebase通知的图标,如下所示:

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/myLogo" />
// Here, instead of myLogo you can put the drawable you want.

Setting small notification icon dynamically is not possible currently in Android, only large icon can be set this way. 当前在Android中无法动态设置小通知图标,只能以这种方式设置大图标。 You need to store the drawables of the relevant small icons locally and use the appropriate one when building the notification. 您需要在本地存储相关小图标的可绘制对象,并在构建通知时使用适当的图标。 Sample: 样品:

 // URL value is your URL to the image
Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"mynotification")
        .setContentTitle(title)
        .setSmallIcon(R.drawable.myLogo)
        .setLargeIcon(mIcon1)
        .setAutoCancel(true)
        .setContentText(message);

Instead of using URL, you can load the large icon from anywhere as a bitmap but not the small icon because setSmallIcon() which requires id to a locally stored resource as from it's definition: 除了可以使用URL之外,您还可以从任何地方将大图标作为位图加载,而不能将小图标作为位图加载,因为setSmallIcon()从定义开始就需要id到本地存储的资源:

/**
 * Set the small icon to use in the notification layouts.  Different classes of devices
 * may return different sizes.  See the UX guidelines for more information on how to
 * design these icons.
 *
 * @param icon A resource ID in the application's package of the drawable to use.
 */
public Builder setSmallIcon(int icon) {
    mNotification.icon = icon;
    return this;
}

When the app is in the background the launcher icon is used from the manifest (with the requisite Android tinting) for messages sent from the console. 当应用程序在后台运行时,清单中的启动器图标(带有必要的Android色彩)用于从控制台发送的消息。

Apparently you see the icon specified in the manifest for your application 显然,您会看到清单中为您的应用程序指定的图标

Look here and here 这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Firebase 推送通知图标 - flutter firebase push notification icon Firebase功能用于从Firebase DB获取数据以进行推送通知 - Firebase function to fetch data from Firebase DB to make Push notification 如何从Firebase推送通知中获取字幕 - How to get subtitle from firebase push notification Firebase 推送通知应用程序图标丢失 - Firebase push notification app icon is missing 如何自定义推送通知图标? - How to customize push notification icon? 如何发送推送通知Firebase - How to send Push Notification Firebase 我们如何使用Firebase云消息传递(fcm)在Android中发送推送通知来发送图像和图标 - How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android 如何使用自定义点击操作从Firebase控制台发送推送通知 - How to send Push Notification With Custom Click Action From Firebase Console 如何使用谷歌应用脚​​本从 Firebase 发送推送通知? - How to send push notification from Firebase using google apps script? 如何以编程方式从状态栏 android 清除 firebase 推送通知 - How to clear firebase Push notification from status bar android programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM