简体   繁体   中英

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. 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:

    <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. 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:

/**
 * 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.

Apparently you see the icon specified in the manifest for your application

Look here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM