簡體   English   中英

如何從Firebase獲取推送通知上的圖標?

[英]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;
}

當應用程序在后台運行時,清單中的啟動器圖標(帶有必要的Android色彩)用於從控制台發送的消息。

顯然,您會看到清單中為您的應用程序指定的圖標

這里這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM