gpt4 book ai didi

android - 使用 Firebase 云消息传递显示大 View 通知

转载 作者:行者123 更新时间:2023-11-30 00:34:47 25 4
gpt4 key购买 nike

Firebase Cloud Messaging documentation ,没有提到大 View /扩展布局的通知。

当应用程序处于后台时,我应该如何显示大 View 通知?根据此 faq,在 FirebaseMessagingServiceonMessageReceived 中创建自定义通知似乎是不可能的:

When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification.

最佳答案

使用数据对象发送您想要查看的通知。您基本上可以将所需的所有内容放入数据对象中,并始终在 onMessageReceived 方法中接收它。这是一个例子。

public class AppFireBaseMessagingService extends FirebaseMessagingService {

private final static int REQUEST_CODE = 1;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (data == null) return;

if (data.containsKey("title") && data.containsKey("message")) {
showNotification(data.get("title"), data.get("message"));
}
}

private void showNotification(String title, String body) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.drawable.notification_icon);

if (body != null && !body.isEmpty()) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
builder.setContentText(body);
}

Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = builder.build();
n.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(0, n);
}

}

关于android - 使用 Firebase 云消息传递显示大 View 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600569/

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