[英]How to fetch icon on Push Notification from Firebase?
在此处输入图像描述,我想从Firebase的“推送通知”中获取不同图标,但是每次都可以看到默认图标。 所以我该怎么做?
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());
}
}
确保在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.
当前在Android中无法动态设置小通知图标,只能以这种方式设置大图标。 您需要在本地存储相关小图标的可绘制对象,并在构建通知时使用适当的图标。 样品:
// 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);
除了可以使用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.