1

後,我嘗試使用火力地堡推送通知的Android。但是我面對很奇怪的問題。我在前臺發送推送時一切正常。當我在後臺發送推送信息(我僅發送數據)時,一切正常,直到我回到前臺,然後再回到後臺。FirebaseMessagingService不叫第二個背景

我時,我有我的應用程序進入到第二次背景FirebaseMessagingService不叫。另外,請注意我使用Android模擬器。代碼:

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService { 
    private static final String TAG = "MyAndroidFCMService"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    } 
} 

//... 

public class MyAndroidFirebaseInstanceIdService extends FirebaseInstanceIdService { 

    private static final String TAG = "MyAndroidFCMIIDService"; 

    @Override 
    public void onTokenRefresh() { 
     //Get hold of the registration token 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     //Log the token 
     Log.d(TAG, "Refreshed token: " + refreshedToken); 
    } 
    private void sendRegistrationToServer(String token) { 
     //Implement this method if you want to store the token on your server 
    } 
} 

的AndroidManifest.xml:

<service android:name=".MyAndroidFirebaseMsgService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
     <service android:name=".MyAndroidFirebaseInstanceIdService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

服務器:

curl -H "Content-type: application/json" -H "Authorization:key=<MYKEY>" -X POST -d '{"to": "<MYTOKEN>","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send 

我怎樣才能解決呢?

回答

0

這是工作爲目的,只有當你的應用程序是在前臺通知郵件發送到您的onMessageReceived回調。如果您的應用程序處於後臺或關閉狀態,則會在通知中心顯示通知消息,並且該消息中的任何數據都將傳遞到由於用戶點擊通知而啓動的意圖。

您可以指定一個click_action指示何時通知被用戶點擊這應該是啓動的意圖。如果未指定click_action,則使用主要活動。

當意圖推出可以使用

getIntent()getExtras()。 來檢索一個包含與通知消息一起發送的任何數據的Set。

更多有關通知消息https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive

+0

FirebaseMessagingService不會在後臺 –

+0

如果調用(!getIntent()getExtras()= NULL){ 爲(String鍵:。getIntent()getExtras()鍵設置( )){ Object value = getIntent()。getExtras()。get(key); Log.d(「MainActivity:」,「Key:」+ key +「Value:」+ value); }} 你 –

+0

可以從意向對象 –

相關問題