当应用处于后台或关闭状态时,无法从Firebase消息服务将记录插入SQLite数据库

时间:2016-09-26 06:08:58

标签: android firebase android-sqlite firebase-notifications

我正在尝试使用Firebase通知。我能够使用this文档使通知正常工作。收到消息后,我就可以从MyFirebaseMessagingService服务类中向通知栏发送通知。即使应用程序处于后台或关闭状态,也会发生这种情况。

我需要的是收集通知中发送的数据并将其插入SQLite数据库。如果应用程序位于前台,我编写的代码可以正常工作,但如果它关闭或在后台运行则不起作用。这是我为插入写的内容。

DbHelper dbh=new DbHelper(this,"sample.sqlite",null,1);
SQLiteDatabase sdb=dbh.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("id","1");
cv.put("name","testname");
sdb.insert("test",null,cv);
sdb.close();
dbh.close();

感谢您的任何帮助。提前谢谢。

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

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

public class MyFirebaseMessagingService extends FirebaseMessagingService
{
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.i("Tag","inside message" );
        Log.i(StaticInfo.INFO, "From: " + remoteMessage.getFrom());
        Log.i(StaticInfo.INFO, "Notification Message Title  : " + remoteMessage.getNotification().getTitle());
        Log.i(StaticInfo.INFO, "Notification Message Body   : " + remoteMessage.getNotification().getBody());

        insertPromotion();
        sendNotification(remoteMessage.getNotification().getBody());
    }

    private void sendNotification(String messageBody) 
    {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

    private void insertPromotion() 
    {
        DbHelper dbh = new DbHelper(this, "sample.sqlite", null, 1);
        SQLiteDatabase sdb = dbh.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("id","1");
        cv.put("name","testname");
        sdb.insert("test", null, cv);
        sdb.close();
        dbh.close();

        Log.i("Tag","db closed");
    }

}

2 个答案:

答案 0 :(得分:4)

只有当应用位于前台时,通知才会发送到您应用的onMessageReceived。当您的应用程序背景或未运行时,系统将处理通知并将其显示在系统托盘中。

Firebase documentation将其解释为:

  

通知消息 - FCM代表客户端应用自动向最终用户设备显示消息。通知消息具有一组预定义的用户可见键。

     

数据消息 - 客户端应用负责处理数据消息。数据消息只有自定义键值对。

由于您希望始终调用代码,因此您需要发送数据消息。您无法从Firebase控制台发送数据消息。但是,如果您已经从应用服务器发送消息,则发送数据消息和通知消息的过程与此相同。唯一的区别在于JSON结构,其中数据消息没有notification对象。来自documentation on data messages

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

答案 1 :(得分:0)

要将onMessgeReceived()上收到的数据保存到SQLite数据库,即使您的应用程序在后台(没有活动正在运行),您也可以执行以下操作:

1)创建一个扩展IntentService的类,例如:

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)从onMessageReceived()方法或Firebase服务扩展中的方法启动服务类,例如:

@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)在AndroidManifest.xml中注册服务:

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

有关更深入的说明,请参阅https://guides.codepath.com/android/Starting-Background-Services