[英]How to get data from firebase-message while Android app is in background
我正在使用 firebase 控制台发送 firebase 消息。 消息应包含如下所示的附加数据,目的是在我的应用程序中的 webview 中打开特定的 URL:
我设置了我的清单和 firebase class 来获取消息。 在我的 firebase class 中,我尝试获取数据:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(remoteMessage.getData().containsKey("key1")) {
intent.putExtra("destination", remoteMessage.getData().get("key1"));
}
PendingIntent pendingIntent = PendingIntent.getActivity
(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String channelId = "default";
NotificationCompat.Builder builder;
if (remoteMessage.getNotification() != null ) {
if (remoteMessage.getNotification().getTitle() != null) {
builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
} else {
builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "default", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
}
在我的 MainActivity class 中,我尝试获取数据。 当应用程序在前台时,以下工作(无论打开什么活动,它都会跳转到 MainActivity 并执行以下内容):
@Override
protected void onNewIntent(Intent intent) {
if (intent.getExtras() != null) {
Bundle extras = intent.getExtras();
if(extras.containsKey("destination")) {
Log.e("FIREBASE_CONTAINS", (String) extras.get("destination"));
}
}
}
但是如果应用程序从后台启动,则不会触发该事件。 我试图获取意图并检查活动的 onResume() 事件中的密钥,但它不起作用(在 inCreate() 和 onStart() 中相同)。
有谁能够帮我?
- - - - - - 编辑 - - - - - - - - -
如评论之一所述,问题似乎是 Notification-Messages 不会到达 onMessageReceived() 事件。 显然 firebase 控制台无法发送数据通知(这将到达事件)所以我尝试使用 POSTMAN。 我已经读到我必须将通知标签从消息正文中删除,并将我的所有信息放在数据部分中。 但是如果我这样做,消息将不会到达我的应用程序(当我再次添加通知部分时它们会到达,但在这种情况下它们当然不会到达 onMessageReceived() 事件)。
推送消息有3种类型
推送消息基本上是json负载:
payload:{
notificacion...
data
}
每种类型的推送消息的规则都不同。 在您的情况下,您正在使用Firebase Web控制台并添加自定义数据,这意味着您的有效负载将具有通知和数据。
对于组合类型,背景中的行为是使用默认通知(NotificationCompat,视觉种类)并打开清单中注册的默认活动。 在活动中,您可以获取数据。
假设您的默认活动称为MainActivity
public class MainActivity {
onCreate...{
//... usual stuff
Intent fcmIntent = getIntent();
if fcmIntent != null
//check the extras and forward them to the next activity if needed
}
}
推送消息有两种类型
(1)通知消息(当应用程序处于前台时会收到)
(2)数据消息(当应用程序在后台+前景时将收到)
参考: https : //firebase.google.com/docs/cloud-messaging/android/receive
需要在firebase通知数据集中设置click_action才能从后台接收数据,实现onMessageReceived处理前台数据
在此处查看更新的答案: https://stackoverflow.com/a/73724040/7904082
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.