【发布时间】:2020-03-15 22:00:32
【问题描述】:
我有一个 SharedPreference:sharedPref.getBoolean("online", false)
如果我在online 为假时收到通知,我想阻止它。如果用户离线,通常不会有通知,但我偶尔会收到通知,我希望将此作为备份以防止通知。
通知是通过Firebase Cloud Messaging (FCM) 发送的,这是我的应用程序中的处理方式:
class CustomApplication : Application() {
val MATCH_CHANNEL_ID = "MATCH_CHANNEL"
companion object {
var database: AppDatabase? = null
}
override fun onCreate() {
super.onCreate()
val settings: FirebaseFirestoreSettings = FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build()
FirebaseFirestore.getInstance().firestoreSettings = settings
CustomApplication.database = Room.databaseBuilder(this, AppDatabase::class.java, "AppDatabase").build()
createNotificationChannel()
}
private fun createNotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val matchChannel = NotificationChannel(MATCH_CHANNEL_ID, "Nearby matches", NotificationManager.IMPORTANCE_HIGH)
matchChannel.description = "Nearby matches"
val manager: NotificationManager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(matchChannel)
}
}
}
如果 online 在 SharedPreferences 中为 false,我有什么办法可以阻止通知进入?
编辑:
当我在后台收到通知 FCM 时,它会调用 . FirebaseMessagingService.onCreate():
class CustomFirebaseMessagingService : FirebaseMessagingService() {
val db = FirebaseFirestore.getInstance()
override fun onCreate() {
Log.d(TAG, "CustomFirebaseMessagingService onCreate()")
return
super.onCreate()
}
我已经覆盖 onCreate() 以在 super.onCreate() 之前返回 - 希望取消通知。但是,通知仍然会触发。
CustomFirebaseMessagingService() 中是否有另一个函数可以覆盖来拦截通知并取消它?
【问题讨论】:
-
你可以将通知管理器放在 if 语句中
-
怎么样?
manager.createNotificationChannel(matchChannel)不只是创建通知通道而不处理未来的通知吗? -
您是否使用了扩展 FirebaseMessagingService 的服务?
-
@Zorgan
createNotificationChannel仅在应用程序的通知设置中创建用户可以自定义的Channel(系统也是),但是创建通知是一个不同的过程,您可以按照建议找到它FirebaseMessagingService 中的其他 -
设置标志并检查它是否来自firebase或共享首选项..
标签: android firebase kotlin firebase-cloud-messaging