收到Firebase消息后,在RecyclerView适配器中添加列表项 [英] Add list item in RecyclerView Adapter on receiving Firebase message

查看:247
本文介绍了收到Firebase消息后,在RecyclerView适配器中添加列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase消息传递服务进行消息传递和通知。我似乎无法将来自服务的传入消息传递给适配器,以便在收到消息时将其插入到RecycleView列表中。

我尝试使用BroacastIntent,如下所示:

  public class messaging extends FirebaseMessagingService {
$ b $ @覆盖
public void onMessageReceived(RemoteMessage m){
store(m.getData());
broadcastIntent();


public void broadcastIntent(){
Intent intent = new Intent();
intent.setAction(com.myApp.CUSTOM_EVENT);
sendBroadcast(intent);


code

$ b

和Adpter

  public class ConvoAdapter extends RecyclerView.Adapter< ConvoHolder> {

私人列表< Message>列表;
私人活动A;

公共ConvoAdapter(列表与LT;消息>数据){

}

@覆盖
公共ConvoHolder onCreateViewHolder(ViewGroup中父母,INT viewType ){
View v = LayoutInflater.from(parent.getContext())。inflate(layout,parent,false);
返回新的ConvoHolder(v);

$ b @Override
public void onBindViewHolder(ConvoHolder h,int Position){
final Message M = list.get(Position);
h.config(A,M);


@Override
public int getItemCount(){
return list.size();

$ b public class MyReceiver extends BroadcastReceiver {
@Override $ b $ public void onReceive(Context context,Intent intent){
Toast.makeText(context, Intent Detected,Toast.LENGTH_LONG).show();





清单。

 < receiver android:name =。fragments.chats.ConvoAdapter $ MyReceiver
android :enabled =true
android:exported =false>
< intent-filter>
< / action>
< / intent-filter>
< / receiver>

正因如此,广播接收器没有收到任何消息。

我也会使用任何其他方法,不涉及使用广播接收器。

解决方案

您的流程或体系结构不是标准的做法。标准流程应该是


  1. Firebase服务 $ b $ b
  2. 使用 LocalBroadcastManager
  3. 的$ BroadcastReceiver 的某些活动或片段






1。 Firebase服务

 公共类消息传递扩展FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage m){
store(m.getData());
broadcastIntent();


public void broadcastIntent(){
Intent intent = new Intent();
intent.setAction(com.myApp.CUSTOM_EVENT);
//当我们需要INTRA app
//通信
时,我们应该使用LocalBroadcastManager LocalBroadcastManager.getInstance(YOUR_CONTEXT).sendBroadcast(intent);


$ / code $ / pre

2 /活动


$ b


  1. 注册 Receiver c $ c> Service

      public void onCreate(Bundle savedInstance){
    //您代码的剩余部分
    IntentFilter if = new IntentFilter(com.myApp.CUSTOM_EVENT);
    LocalBroadcastManager.getInstance(this).registerReceiver(onMessage,if);

    $ / code $ $ $ $ $ $ $ $ $ $ $ $接收者$ / code $ c $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' {
    @Override
    public void onReceive(Context context,Intent intent){
    //使用notifyItemInserted(position)更新您的RecyclerView。

    $ / code $ / pre
    $ b $ p
    $ / ol>

    结束 Service 发送本地广播到 code>然后接收它并使用 RecyclerView 实例


    更新或添加项目

    I'm using the firebase messaging service for messaging and notifications. I can't seem to be able to pass an incoming message from the service to the adapter so that when the message is received it can be inserted into the RecycleView List.

    I tried using BroacastIntent as follows :

    public class messaging extends FirebaseMessagingService {
    
       @Override
       public void onMessageReceived(RemoteMessage m) {
          store(m.getData());
          broadcastIntent();
       }
    
       public void broadcastIntent() {
          Intent intent = new Intent();
          intent.setAction("com.myApp.CUSTOM_EVENT");
          sendBroadcast(intent);
       }
    }
    

    and in the Adpter

    public class ConvoAdapter extends RecyclerView.Adapter<ConvoHolder> {
    
       private List<Message> list;
       private Activity      A;
    
       public ConvoAdapter(List<Message> data) {
    
       }
    
       @Override
       public ConvoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
          return new ConvoHolder(v);
       }
    
       @Override
       public void onBindViewHolder(ConvoHolder h, int Position) {
          final Message M = list.get(Position);
          h.config(A, M);
       }
    
        @Override
        public int getItemCount() {
           return list.size();
        }
    
        public class MyReceiver extends BroadcastReceiver {
              @Override
              public void onReceive(Context context, Intent intent) {
                 Toast.makeText(context, "Intent Detected.",    Toast.LENGTH_LONG).show();
              }
        }
    }
    

    And manifest.

    <receiver android:name=".fragments.chats.ConvoAdapter$MyReceiver"
                      android:enabled="true"
                      android:exported="false" >
                <intent-filter>
                    <action android:name="android.intent.action.CUSTOM_EVENT">
                    </action>
                </intent-filter>
            </receiver>
    

    As is, the broadcast receiver is not receiving any messages.

    I'd also use any other method that doesn't involve using broadcast receivers.

    解决方案

    The flow or architecture of your is not a standard practice.

    The standard flow should be

    1. Firebase service
    2. Some activity or fragment with BroadcastReceiver using LocalBroadcastManager


    1. Firebase Service

    public class messaging extends FirebaseMessagingService {
    
       @Override
       public void onMessageReceived(RemoteMessage m) {
          store(m.getData());
          broadcastIntent();
       }
    
      public void broadcastIntent() {
          Intent intent = new Intent();
          intent.setAction("com.myApp.CUSTOM_EVENT");
          // We should use LocalBroadcastManager when we want INTRA app
          // communication
          LocalBroadcastManager.getInstance(YOUR_CONTEXT).sendBroadcast(intent);
      }
    }
    

    2. Activity

    1. Registering Receiver for broadcast from Service

       public void onCreate(Bundle savedInstance) {
             // REST OF YOUR CODE
             IntentFilter if= new IntentFilter("com.myApp.CUSTOM_EVENT");
             LocalBroadcastManager.getInstance(this).registerReceiver(onMessage, if);
        }
      

    2. Writing the Receiver in Activity

      private BroadcastReceiver onNotice= new BroadcastReceiver() {        
            @Override
            public void onReceive(Context context, Intent intent) {        
                   // Update your RecyclerView here using notifyItemInserted(position);
            }
      

      };

    Summary: The Service sends local broadcast to Activity which in turn receives it and updates or add items using RecyclerView instance

    这篇关于收到Firebase消息后,在RecyclerView适配器中添加列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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