android应用程序处于后台时如何从Firebase消息中获取数据 [英] How to get data from firebase-message while android app is in background

查看:328
本文介绍了android应用程序处于后台时如何从Firebase消息中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用firebase-console发送firebase消息.消息中应包含如下所示的其他数据,目的是在我的应用程序的Web视图中打开特定的网址:

I'm sending firebase-messages using the firebase-console. The messages shall contain additional data like shown below with the purpose to open a specific url within a webview in my app:

我设置了清单文件和firebase类来获取消息.在我的Firebase类中,我尝试获取数据:

I set up my manifest and firebase class to get the messages. Within my firebase class i try to get the data:

@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并执行以下操作):

Within my MainActivity class i try to get the data. When the app is in the foreground, the following works (no matter what activity is opened, it will jump to the MainActivity and execute the following):

@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()相同).

But the event wont trigger if the app started from the background. I tried to get the intent and check for the key within the onResume() event of the activity, but it does not work (Same in inCreate() and onStart()).

有人可以帮助我吗?

------------ EDIT -----------------

------------EDIT-----------------

正如其中一条注释中所述,问题似乎是Notification-Messages不会到达onMessageReceived()事件.显然,firebase控制台无法发送数据通知(将到达事件),因此我尝试使用POSTMAN.我已经读到必须将通知标签放在消息正文之外,并将所有信息都放在数据部分.但是,如果这样做,消息将不会重新获得我的应用程序的权限(当我再次添加通知部分时,它们会恢复,但是在这种情况下,它们当然不会到达onMessageReceived()事件).

As descriped in one of the comments, the poroblem seems to be that Notification-Messages won't reach the onMessageReceived() event. Apparently the firebase console can't send data-notifications (wich would reach the event) so I tried using POSTMAN. I've read that I have to leave the notification tag out of the message body and put all my information in the data section. But if I do so, the messages won't rech my app (they do, when I add the notification section again, but of course they are not reaching the onMessageReceived() event in that case).

推荐答案

推送消息有两种类型

(1)通知消息(当应用程序处于前台时会收到)

(1)Notification Message (will receive when app is in foreground)

(2)数据消息(当应用程序处于后台+前景时会收到)

(2)Data Message (will receive when app is in background+foreground)

参考文献: https://firebase.google.com/docs/cloud -messaging/android/receive

这篇关于android应用程序处于后台时如何从Firebase消息中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆