gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:55 25 4
gpt4 key购买 nike

我正在试用 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");
}

}

最佳答案

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

Firebase documentation解释为:

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 对象。来自documentation on data messages

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

关于android - 当应用程序处于后台或关闭状态时,无法将记录从 Firebase 消息服务插入 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39695743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com