Android Firebase消息传递:如何从onMessageReceived()更新UI [英] Android Firebase Messaging: How Update UI from onMessageReceived()

查看:253
本文介绍了Android Firebase消息传递:如何从onMessageReceived()更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用中成功实现了Firebase消息传递.在后台运行良好,当应用程序在前台(yippee!)时,onMessageReceived()会被调用.

I have successfully implemented Firebase messaging in my app. Works great in the background and onMessageReceived() gets called when the app is in the foreground (yippee!).

我遇到的问题是,我需要动态更新UI,而我仍坚持实现这一目标的最佳方法.我不想向用户发送应用内通知(如示例代码所示),我不确定是否要发送广播,我要做的就是访问MainActivity以调用方法已经存在,但是我没有在服务中引用MainActivity.

The issue I have is that I need to update the UI dynamically and I am stuck on the best way to achieve this. I don't want to send the user an in-app notification (as the sample code shows), I'm not sure I want to send a broadcast, all I want to do is to access the MainActivity in order to call a method already there, however I have no reference to the MainActivity in the service.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification().getBody() != null) {
        Log.d(LOG_TAG, "Message received: " + remoteMessage.getNotification().getBody());
    } else {
        Log.d(LOG_TAG, "Message received: " + remoteMessage.getData().get("message"));
    }

    // Call method in MainActivity
    // <<< What goes Here?>>>>

}

这似乎是一个简单的用例,但我找不到任何在线帮助.

This seems a simple use case but I can't find anything online to help.

预先感谢

推荐答案

是的,您可以使用本地广播

在您的 onMessageReceived() Firebase服务中.

In your onMessageReceived() Firebase Service.

broadcaster = LocalBroadcastManager.getInstance(getBaseContext());

   Intent intent = new Intent(REQUEST_ACCEPT);
   intent.putExtra("Key", value);
   intent.putExtra("key", value);
   broadcaster.sendBroadcast(intent);

并在活动"或片段"方法中注册本地广播

and register local Broadcast in your Activity or fragment method

 @Override
    public void onStart() {
        super.onStart();
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver((receiver),
                new IntentFilter(PushNotificationService.REQUEST_ACCEPT)
        );
    }



    @Override
        public void onStop() {
             super.onStop();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);

        }

并处理这样的更新事件,在这里执行更新UI,当收到通知并onMessageReceived()发送广播时,它将自动调用.

and Handle Your update event like this, do your update UI work Here, it will call automatically when notification received and onMessageReceived() send a broadcast.

receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String   value= intent.getStringExtra("key");
                    String value= intent.getStringExtra("key");

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };

这篇关于Android Firebase消息传递:如何从onMessageReceived()更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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