Unable to insert record into SQLite Database from Firebase Message Service when app is in background or closed state

三世轮回 提交于 2019-12-01 21:53:32

Notifications will be delivered to your app's onMessageReceived only when the app is in the foreground. When your app is backgrounded or not running, the system will handle the notification and display it in the system tray.

The Firebase documentation explains it as:

Notification message - FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.

Data message - Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

Since you want your code to always be invoked, you'll need to send data messages. You cannot send data messages from the Firebase Console. But if you already send messages from an app server, the process for sending data messages and notification messages is the same there. The only difference is in the JSON structure, where a data messages doesn't have a notification object. From the documentation on data messages

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
}

To save data received on onMessgeReceived() to SQLite database even when your app is in the background (no activity running), you can do the following:

1) Create a class that extends IntentService, e.g:

public class SQLService extends IntentService {
    private final static String MESSAGE_ID = "message_id";

    private MySQLiteDbAdapter mySQLiteAdapter;

    public SQLService() {
        super("test-service");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // initialize SQLite adapter here using getApplicationContext()
        this.mySQLiteAdapter = new MySQLiteDbAdapter(getApplicationContext());

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // fetch data to save from intent
        Message message = new Message();
        Message.setMessage_id(intent.getStringExtra(MESSAGE_ID));
        ...
        // save 
        this.mySQLiteAdapter.add(message);

    }
}

2) Launch the service class from onMessageReceived() method or a method within your extension of the Firebase service, e.g.:

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getData() != null) {

            Intent intent = new Intent(this, SQLService.class);
            // add data to intent
            intent.putExtra(MESSAGE_ID, remoteMessage.getData().get(MESSAGE_ID));
            ...
            // start the service
            startService(intent);

        }
    }

3) Register the service in your AndroidManifest.xml:

<application
  ...
        <service
            android:name=".SQLService"
            android:exported="false"/>
  ...

For more in-depth explanation see https://guides.codepath.com/android/Starting-Background-Services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!