2017-06-20 62 views
1

我需要幫助。 Firebase通知不適用於後臺。這是我的代碼:Firebase通知不在後臺工作

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    Log.d(TAG, "FROM:" + remoteMessage.getFrom()); 
    sharedPreference = getSharedPreferences(Global.SECURETRADE, 0); 
    UID = sharedPreference.getString(Global.ID, ""); 


     Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`; 

     NotificationCompat.Builder notificationBuilder = new 
       NotificationCompat.Builder(this); 

     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

      notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon); 

     } else { 

      notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon); 
     } 

     notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon)); 
     notificationBuilder.setContentTitle(remoteMessage.getData().get("title")); 
     notificationBuilder.setContentText(remoteMessage.getData().get("body")); 
     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setSound(defaultSoundUri); 
     notificationBuilder.setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) 
         getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 

    } 




} 
+0

您是如何發送消息的? –

+0

我也想知道它是相同的,之前它正常工作 –

+0

我也想知道爲什麼它不工作,Firebase給出了很多錯誤,這是很有趣的 –

回答

-1

當應用程序在後臺運行onMessageReceive將無法正常工作

離線消息來發射活動剛纔複製的代碼,這個和平的發射活動,並檢查它。它會工作。

Bundle bundle = getIntent().getExtras(); 
    if (bundle != null) { 
     Logger.info(TAG, "FIRE BASE OFF LINE NOTIFICATIONS COMING TO THIS BLOCK--->"); 

     JSONObject json = new JSONObject(); 
     Set<String> keys = bundle.keySet(); 
     for (String key : keys) { 
      Logger.info(TAG, "json object--->" + key + "---values--" + JSONObject.wrap(bundle.get(key))); 
     } 
    } 

,你的有效載荷應該是這樣的

{ "notification": { 
    "title": "Your Title", 
    "text": "Your Text", 
    "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter 
    }, 
    "data": { 
    "keyname": "any value " //you can get this data as extras in your activity and this data is optional 
    }, 
    "to" : "to_id(firebase refreshedToken)" 
} 

reference

0

你發送數據的消息(不通知的消息)?

通知的消息當你想FCM處理顯示代表您的客戶端應用程序的一個 通知不叫onMessageReceived()

通知郵件。 想要處理客戶端應用程序中的消息時使用數據消息。

在這裏閱讀更多:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

PS:FCM Web控制檯始終發送通知的消息


如果您發送的是數據的消息,並onMessageReceived()不叫..
那麼這是一個不同的問題。

它甚至可能是特定設備的問題。
請參閱Push notifications using FCM not received when app is killed android