[英]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 显然,您会看到清单中为您的应用程序指定的图标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.