要在Android Api 29上启动器活动中获取FCM通知的额外数据,您可以使用以下解决方法:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// 获取额外的数据
Map extraData = remoteMessage.getData();
// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
// 将额外的数据添加到通知中
for (Map.Entry entry : extraData.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
builder.addData(key, value);
}
// 显示通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
onCreate
方法中,检查是否存在额外的数据,并根据需要进行处理。以下是一个示例:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
// 检查是否存在额外的数据
Bundle extras = getIntent().getExtras();
if (extras != null) {
// 获取额外的数据
String extraData = extras.getString("extra_data");
// 在此处处理额外的数据
// ...
}
}
请注意,您需要根据您的实际需求修改代码中的变量和逻辑。此示例假定您已经设置了适当的通知通道和权限。