首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当Android应用程序处于后台时如何从firebase消息中获取数据

当Android应用程序处于后台时如何从firebase消息中获取数据
EN

Stack Overflow用户
提问于 2019-07-17 13:24:52
回答 3查看 1.9K关注 0票数 1

我在用火柴控制台发送火源信息。这些消息将包含如下所示的附加数据,其目的是在我的应用程序中的webview中打开一个特定的URL:

我设置了我的清单和火基类来获取消息。在我的firebase类中,我尝试获取数据:

代码语言:javascript
复制
@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类中,我尝试获取数据。当应用程序位于前台时,以下操作(无论打开什么活动,它都会跳转到MainActivity并执行以下操作):

代码语言:javascript
复制
@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()中也一样)。

有人能帮我吗?

??编辑??编辑?

正如其中一个注释所描述的,问题似乎在于通知-消息不会到达onMessageReceived()事件。显然,消防控制台无法发送数据通知(这将到达事件),所以我尝试使用邮递员。我已经读到,我必须将通知标记从消息体中删除,并将我的所有信息放在data部分中。但是如果我这样做了,消息就不会到达我的应用程序(当我再次添加通知部分时,它们会到达,但在这种情况下,它们当然不会到达onMessageReceived()事件)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-17 13:47:14

有两种推送消息。

(1)通知信息(当app在前台时将收到)

(2)数据消息(当app在background+foreground中时将接收)

参考资料:https://firebase.google.com/docs/cloud-messaging/android/receive

票数 0
EN

Stack Overflow用户

发布于 2019-07-17 13:46:01

推消息有三种类型

  • 通知
  • 数据
  • 两者都是

推送消息基本上是一个json有效负载:

代码语言:javascript
复制
payload:{
    notificacion...
    data
}

每种类型的推送消息的规则是不同的。在您的情况下,您正在使用Firebase web控制台并添加自定义数据,这意味着您的有效负载将有通知和数据。

对于组合类型,背景中的行为是使用默认通知(NotificationCompat,可视类型)并打开在清单中注册的默认活动。在活动中,您可以获得数据。

让我们假设您的默认活动称为MainActivity

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2022-09-14 23:29:48

您需要在firebase通知数据集中设置click_action,以便能够从后台接收数据,并实现处理前台数据的onMessageReceived。

见此处更新的答案:https://stackoverflow.com/a/73724040/7904082

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57076964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档