要在安卓应用中使用Firebase云消息通知图标,您可以按照以下步骤操作:
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.10'
}
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-messaging:22.0.0'
}
apply plugin: 'com.google.gms.google-services'
请确保将ic_notification_icon替换为您应用中的实际图标资源。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// 获取消息数据
String title = remoteMessage.getData().get("title");
String message = remoteMessage.getData().get("message");
// 创建通知
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true);
// 显示通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notificationBuilder.build());
}
}
请确保将channel_id替换为您自定义的通知通道ID,并将ic_notification_icon替换为实际图标资源。
通过按照以上步骤操作,您的安卓应用就可以使用Firebase云消息通知图标了。请记得替换示例代码中的资源和通道ID,以适应您的应用。