2016-07-08 63 views
0

我在使用FirebaseMessagingService時遇到問題。 FirebaseMessagingService工作正常。我收到消息和通知。但是我需要將消息發送給Activity,並在Activity中調用一個方法。FirebaseMessagingService單身?控制實例

我試着創建一個偵聽器,但是當收到一條消息時,創建了一個新的實例,並且偵聽器爲空。據我所知,FirebaseMessagingService有一個消息接收時instanciated。所以我想在單身人士和聽衆一起,不工作,聽衆保持空。

有人有一個想法,我可以如何發送消息,打開活動?

+0

把一些代碼會好得多。 –

回答

1

我不認爲這需要任何聽衆。一旦您收到onMessageReceived()的消息,只需使用LocalBroadcastmanager進行廣播。並在您的活動中接收該廣播。

下面的代碼片段,以達到你想要的東西:

MyFirebaseMessagingService.java:

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
     Intent intent = new Intent(**Action**); 
     intent.putExtra("Some Payload", message.getBody()); 
     mLocalBroadcastManager.sendBroadcast(intent); 
} 

MainActivity.java:

private LocalBroadcastManager mLocalBroadcastManager; 
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

    } 
}; 

@Override 
protected void onStart() { 
    super.onStart(); 
    registerBroadcastReceiver(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    unregisterBroadcastReceiver(); 
} 

private void registerBroadcastReceiver() { 
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); 
    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(**Action**); 
    mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter); 
} 

private void unregisterBroadcastReceiver() { 
    mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver); 
} 
+0

我會試試這個,謝謝 – novac

0

要小心,雖然完全不超過限制數據大小可以使用intent來傳遞 這裏是指 https://code.google.com/p/android/issues/detail?id=5878

我明白你的問題沒有任何代碼。這幾乎是你應該工作的一個交鑰匙解決方案。我使用這種方法在服務和活動之間傳遞數據。

您首先需要將要傳遞的數據轉換爲可以通過意圖傳遞的數據。

字符串很容易處理,所以轉換爲字符串並放入一個意圖。在使用onCreate和onNewItent的活動端,您可以收到這些數據沒有問題。然後將其轉換回來,無論您希望如何。請參閱下面的代碼以獲取示例。

如果接收方不是unRegistered,使用廣播接收方可能會給你數據泄漏。它會發生,如果你的應用程序崩潰,並且unRegister不被告知關閉。

在你FirebaseMessagingService類

import android.os.AsyncTask; 
    import com.google.firebase.messaging.FirebaseMessagingService; 
    import com.google.firebase.messaging.RemoteMessage; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import java.util.Map; 

    /** 
    * Created by acopp 
    * Date: 12/31/2016. 
    * Time: 1:41 PM 

    * You have permission to use this file for any reason that is not for evil doing 
    */ 

    public class FBMService extends FirebaseMessagingService { 
     static String TAG = "FBMService"; 
     static String FBMServiceAction = "FBMService.Action"; 

     @Override 
     public void onMessageReceived(RemoteMessage remoteMessage) { 
      MainActivity.passIntent(this,FBMServiceAction,getString(remoteMessage)); 
     } 
     String getString(RemoteMessage message){ 
      Map<String, String> messageData = message.getData(); 
      JSONObject j = new JSONObject(); 
      for (String key : messageData.keySet()) { 
       String value = messageData.get(key); 
       try { 
        j.put(key, value); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      return j.toString(); 
     } 

    } 

活動課

//In your activity class 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import java.util.HashMap; 
    import java.util.Iterator; 
    import java.util.Map; 

    /** 
    * Created by acopp 
    * Date: 12/31/2016. 
    * Time: 1:41 PM 
    * You have permission to use this file for any reason that is not for evil doing 
    */ 

    public class MainActivity extends Activity { 
     private String TAG = "MainActivity"; 

     //Call this from FBMService to start your activity or if your activity is start to receive a new intent 
     static void passIntent(Context context, String action, String messageDataString) { 
      Intent intent = new Intent(context, MainActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.putExtra("action", action); 
      intent.putExtra("message", messageDataString); 
      context.startActivity(intent); 
     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      Log.d(TAG, "onCreate"); 
      intentHandler(getIntent()); 
     } 
     @Override 
     protected void onNewIntent(Intent intent) { 
      super.onNewIntent(intent); 
      Log.d(TAG, "onNewIntent"); 
      intentHandler(intent); 
     } 
     //Use an intent handler to manage onNewIntent and onCreate the same way 
     void intentHandler(Intent intent) { 
      if (intent.hasExtra("action")) { 
       String action = intent.getStringExtra("action"); 
       if(action.equals(FBMService.FBMServiceAction)){ 
        if (intent.hasExtra("message")) { 
         String messageDataString = intent.getStringExtra("message"); 
         new iterEat().execute(messageDataString); 
        } 
       } 
      } 
     } 
     //Convert your string to a HashMap in the background off the main thread 
     class iterEat extends AsyncTask<String,Void,Map<String, String> > { 
      @Override 
      protected Map<String, String> doInBackground(String... rm) { 
       String messageDataString = rm[0]; 
       try{ 
        return fromString(messageDataString); 
       }catch (NullPointerException e){ 
        return null; 
       } 
      } 
      @Override 
      protected void onPostExecute(Map<String, String> s) { 
       //Your data is pooped out here 
       Map<String, String> messageData = s;//PLOP 
      } 
     } 

     Map<String, String> fromString(String jsonString) throws NullPointerException{ 
      try { 
       Map<String, String> messageData = new HashMap<>(); 
       JSONObject j = new JSONObject(jsonString); 
       Iterator<String> i = j.keys(); 
       while(i.hasNext()){ 
        String key = i.next(); 
        String value = j.getString(key); 
        messageData.put(key,value); 
       } 
       return messageData; 

      } catch (JSONException e) { 
       throw new NullPointerException("Didn't work"); 
      } 
     } 
    }