问题描述
我正在使用 FirebaseMessagingService
向我的 Pocophone X3 NFC 发送推送通知。但是,当登录和注销发生时,我将首先发送通知令牌并从我的 Firestore 数据库中删除通知令牌。这是为了确保将通知发送给正确的用户。
@AndroidEntryPoint
class MessagingService :
FirebaseMessagingService(),FirebaseAuth.AuthStateListener {
@Inject
lateinit var notificationRepository: NotificationRepository
override fun onCreate() {
super.onCreate()
FirebaseAuth.getInstance().addAuthStateListener(this)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val title = remoteMessage.notification?.title
val message = remoteMessage.notification?.body
if (message != null && title != null) {
sendPushNotification(title,message)
}
}
override fun onNewToken(token: String) {
savetokenToSharedPreferences(token)
authRepository.getUser()?.let {
val uid = it.uid
sendTokenToFirestore(uid,token)
}
}
override fun onAuthStateChanged(auth: FirebaseAuth) {
auth.currentUser?.let {
val uid = it.uid
val savedRegistrationToken =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.fcm_token_shared_pref_key),"")
savedRegistrationToken?.let { token -> sendTokenToFirestore(uid,token) }
}
}
private fun sendPushNotification(title: String,messageBody: String) {
val intent = Intent(this,MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this,REQUEST_CODE,intent,PendingIntent.FLAG_UPDATE_CURRENT)
val channelId = getString(R.string.general_notification_channel_id)
val defaultSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.mlearn_logo)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationmanager = getSystemService(Context.NOTIFICATION_SERVICE) as notificationmanager
val channel = NotificationChannel(
channelId,getString(R.string.general_notification_channel_name),notificationmanager.IMPORTANCE_DEFAULT
)
notificationmanager.createNotificationChannel(channel)
notificationmanager.notify(NOTIFICATION_ID,notificationBuilder.build())
}
private fun savetokenToSharedPreferences(token: String) {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
with (sharedPref.edit()) {
putString(getString(R.string.fcm_token_shared_pref_key),token)
apply()
}
}
private fun sendTokenToFirestore(uid: String,token: String) {
GlobalScope.launch(dispatchers.IO) {
notificationRepository.sendNotificationToken(uid,token)
}
}
companion object {
const val REQUEST_CODE = 0
const val NOTIFICATION_ID = 0
}
}
我也像这样将服务注册到清单中。
<service
android:name=".service.MessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
代码运行良好当我第一次安装应用程序时,如果我没有重新启动任何设备或终止应用程序。然而,当我要么
- 关掉我的手机,然后再打开;或
- 从我最近使用的应用列表中删除该应用
FirebaseMessagingService
代码将不再运行。因此,当我使用新帐户登录时,新帐户没有关联的通知令牌,因为服务未运行。
如何使 FirebaseMessagingService
即使在应用程序被终止以及手机关机和开机(即重新启动)的情况下也能继续运行?请注意,安装的应用尚未签名。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)