2017-09-16 93 views
0

FCM服务。 NotificationDatabaseHandler是助手类。保存消息标题和当前时间。当应用程序处于后台时,无法成功保存FCM消息,因为它在应用程序处于前台时正常工作

public class ApplicationFCMService extends FirebaseMessagingService { 

    private static final String TAG = ApplicationFCMService.class.getName(); 
    private NotificationUtils notificationUtils; 
    private NotificationDatabaseHandler databaseHandler; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 

     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     databaseHandler = new NotificationDatabaseHandler(getApplicationContext()); 

     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
      handleNotification(remoteMessage.getFrom(), remoteMessage.getNotification().getBody()); 

      databaseHandler.addNotification(remoteMessage.getNotification().getBody(), getDate()); 
      // Log.d("FCM", messagesSet.toString()); 
      Log.d("FCM", remoteMessage.getNotification().getBody()); 
     } 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

      try { 
       JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
       handleDataMessage(json); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 
     } 
    } 
+0

检查这个答案:https://stackoverflow.com/questions/45875859/fcm-onmessagereceived-not-calling-android/45880920#45880920 – Maddy

+0

https://stackoverflow.com/questions/45267335/create-an-repetitive - 当应用程序未运行时发生高度警报 - 远程触发/ 45406863#45406863 –

+0

有没有任何方法将通知作为Firebase的“数据”发送? –

回答

0

我已经使用邮递员发送数据通知(测试)。在接收到数据消息时,会执行onMessageReceived()方法。 现在,即使应用程序处于后台,我也可以将通知保存在数据库中。

相关问题