1

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.

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52
Relm
  • 7,923
  • 18
  • 66
  • 113
  • What you have given us is a BroadCastReceiver class not your adapter. – Rushi Ayyappa Nov 18 '16 at 04:51
  • Yes the Adapter is the enclosing class of the BroadCastReceiver class. I'll edit to enclose it – Relm Nov 18 '16 at 04:55
  • @RushiAyyappa see the new edit. – Relm Nov 18 '16 at 05:01
  • did you try using sqlDataBase?in onMessageReceived() check whether your screen is active or not.if not active store it in sqLiteDataBase and sync db when you open the screen. If the screen is active then create a method that updates the screen and access this ,method by creating an object for adapter class in onMessageReceived().make sure you write this in try and catch blocks – Rushi Ayyappa Nov 18 '16 at 05:08
  • @RushiAyyappa, I honestly don't understand your suggestion, could you write this as an answer and explain in detail. – Relm Nov 18 '16 at 05:16

3 Answers3

9

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

Kuya
  • 7,280
  • 4
  • 19
  • 31
Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52
0

if in case you don't want to use BroadCaseReceiver for your task.Follow below steps:

There are two things to note here.

Firstly,Check whether the Activity is open(Activel visible) or not.

i. When your screen is active.(screen is visible).

1.In your onMessageReceived() store the received message in SQLiteDataBase with time. 2.create a method in your adapter which will update the screen by fetching the data from SQLiteDB. 3.Now whenever a new message is received call this method in Adapter class.

ii.in case the screen is not active:

1.store the received message in sqliteDb and show it as a notification.

note:

1.make sure you write all these in a try catch block. 2.make sure to sync your SQLiteDB on opening the screen for the first time. 3.in case if this does not help you please try this way.. Refreshing activity on receiving gcm push notification

Community
  • 1
  • 1
Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
  • This doesn't address the problem. How will the adapter know that the message is received? – Relm Nov 18 '16 at 05:30
  • you can even create a static method in adapter class and call that method which will update the data. – Rushi Ayyappa Nov 18 '16 at 05:30
  • you have to call this method in onMessageReceived() – Rushi Ayyappa Nov 18 '16 at 05:30
  • A static method in the adapter called from the messaging service won't have access to adapter properties. You wound't be able to call methods like `notifyItemInserted`, unless such methods are also static in the adapter. – Relm Nov 18 '16 at 05:41
  • how about creating an object to that Adapter in onMessageReceived? I have no idea on this.I did above mentioned to update location in my Map project to update location. – Rushi Ayyappa Nov 18 '16 at 05:45
0

I think the above code didnt worked because the action name above given and the one registered in manifest is different

<action android:name="com.myApp.CUSTOM_EVENT">
                </action>

intent.setAction("com.myApp.CUSTOM_EVENT");

give same name i think the above code will work