当应用程序处于后台或关闭状态时,无法从Firebase Message Service将记录插入到SQLite数据库中 [英] Unable to insert record into SQLite Database from Firebase Message Service when app is in background or closed state

查看:73
本文介绍了当应用程序处于后台或关闭状态时,无法从Firebase Message Service将记录插入到SQLite数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying out Firebase Notifications. I was able to get the Notification to work properly using this documentation. The message was received and I was able to send a notification to the Notification Bar from within the MyFirebaseMessagingService service class. This happens even when the app is in background or closed.

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

What I need is to collect the data sent in the notification and insert it into an SQLite database. The code I wrote works fine if the app is in foreground, but it does not work if it is closed or in background. Here is what I wrote for the insert.

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();

对此表示感谢.预先感谢.

Appreciate any help with this. Thanks in advance.

<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");
    }

}

推荐答案

仅当应用程序位于前台时,通知才会传递到应用程序的onMessageReceived.当您的应用程序处于后台运行或未运行时,系统将处理该通知并将其显示在系统托盘中.

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.

Firebase文档解释为:

The Firebase documentation explains it as:

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

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.

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

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"
   },
}

这篇关于当应用程序处于后台或关闭状态时,无法从Firebase Message Service将记录插入到SQLite数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆