Android studio 当应用程序处于后台或终止时,将FCM数据保存在房间中
我正试图将FCM数据保存在房间数据库中,但它只在forgroung的应用程序中起作用。 当应用程序处于后台或被杀死时,通知会出现,但不会保存在数据库中 我看到很多关于这方面的文章,但都不管用 我的通知服务类:Android studio 当应用程序处于后台或终止时,将FCM数据保存在房间中,android-studio,firebase-cloud-messaging,android-room,Android Studio,Firebase Cloud Messaging,Android Room,我正试图将FCM数据保存在房间数据库中,但它只在forgroung的应用程序中起作用。 当应用程序处于后台或被杀死时,通知会出现,但不会保存在数据库中 我看到很多关于这方面的文章,但都不管用 我的通知服务类: class NotificationService : FirebaseMessagingService() { override fun onNewToken(p0: String) { super.onNewToken(p0) } compa
class NotificationService : FirebaseMessagingService() {
override fun onNewToken(p0: String) {
super.onNewToken(p0)
}
companion object {
private const val NOTIFICATION_CHANNEL_ID = "Channel 1"
private const val NOTIFICATION_ID = 100
}
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
if (p0.data.size > 0){
val param: Map<String, String> = p0.data
sendNotification(param)
broadcastNotification()
}
}
private fun sendNotification(param: Map<String, String>) {
val db = AppDatabase.getInstance(applicationContext)
val notificationEntity = NotificationEntity(null,param["title"], param["body"])
db.notificationDao().addNotification(notificationEntity)
val intent = Intent(this, MainActivity::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("postId", param["postId"])
val image = ImageRequest.Builder(applicationContext)
.data(param["img-url"])
.build()
imageLoader.enqueue(image)
val bitmap = param["img-url"]?.let { getBitmapFromUrl(it) }
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notifiBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setStyle(
NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null)
).setLargeIcon(bitmap)
.setContentTitle(param["title"])
.setContentText(param["body"])
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(sound)
.setPriority(NotificationManagerCompat.IMPORTANCE_MAX)
.setContentIntent(pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(NOTIFICATION_ID, notifiBuilder.build())
}
private fun getBitmapFromUrl(url: String): Bitmap? {
try {
val url: URL = URL(url)
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val input: InputStream = connection.inputStream
return BitmapFactory.decodeStream(input)
} catch (e: Exception) {
return null
}
}
private fun broadcastNotification(){
val intnet = Intent("new_notification")
LocalBroadcastManager.getInstance(this).sendBroadcast(intnet)
}
}
@Dao
interface NotificationDao {
@Insert
fun addNotification(notificationEntity: NotificationEntity)
@Query("SELECT * FROM notification_data")
fun getPostList(): List<NotificationEntity>
}
@Database(entities = [PostEntity::class, NotificationEntity::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {
abstract fun postDao(): PostDao
abstract fun notificationDao(): NotificationDao
companion object {
private var INSTANCE: AppDatabase?= null
fun getInstance(context: Context): AppDatabase {
if (INSTANCE == null){
synchronized(AppDatabase::class.java){
INSTANCE = buildRoomDB(context)
}
}
return INSTANCE!!
}
private fun buildRoomDB(context: Context) =
Room.databaseBuilder(
context,
AppDatabase::class.java,
NOTIFICATION_DATABASE
).build()
}
}