gpt4 book ai didi

android - 带有新 Firebase 云消息传递系统的通知图标

转载 作者:IT老高 更新时间:2023-10-28 13:01:40 26 4
gpt4 key购买 nike

昨天,Google 在 Google I/O 上展示了基于新 Firebase 的新通知系统。我用 Github 上的示例尝试了这个新的 FCM(Firebase Cloud Messaging)。

通知的图标始终是 ic_launcher,尽管我已经声明了一个特定的可绘制对象

为什么?下面是处理消息的官方代码

public class AppFirebaseMessagingService extends FirebaseMessagingService {

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage);
}
// [END receive_message]

/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM RemoteMessage received.
*/
private void sendNotification(RemoteMessage remoteMessage) {

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// this is a my insertion looking for a solution
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.myicon: R.mipmap.myicon;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle(remoteMessage.getFrom())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

最佳答案

很遗憾,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。当应用程序在后台时,启动器图标会从 list (带有必要的 Android 着色)中用于从控制台发送的消息。

但是,使用 SDK 9.8.0,您可以覆盖默认值!在您的 AndroidManifest.xml 中,您可以设置以下字段来自定义图标和颜色:

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/google_blue" />

请注意,如果应用程序在前台(或发送数据消息),您完全可以使用自己的逻辑来自定义显示。如果从 HTTP/XMPP API 发送消息,您还可以始终自定义图标。

关于android - 带有新 Firebase 云消息传递系统的通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com