Java用户启用和禁用,通过使用开关启用和禁用推送通知

I'm using firebase push notification(FCM).. and I want to enable and disable notifications using a switch button.

For that I have shared preferences to enable and disable notifications but it seems my logic is not at all working.

It doesn't make any difference if the switch is turn on or off. I am still receiving notifications.

I need help thanks.

activity:--

val sharedPreferences = getSharedPreferences("myname", MODE_PRIVATE)

simpleSwitch.setChecked(sharedPreferences.getBoolean("SWITCH_PARTIDOS_STATE", false))

simpleSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->

sharedPreferences.edit().putBoolean("SWITCH_PARTIDOS_STATE", isChecked).commit()

if (isChecked) {

// FirebaseMessaging.getInstance().subscribeToTopic("Partidos")

Toast.makeText(applicationContext, "Activado Correctamente",

Toast.LENGTH_LONG).show()

} else {

// FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos")

Toast.makeText(applicationContext, "Desactivado Correctamente",

Toast.LENGTH_LONG).show()

}

PreferenceHelper.prefernceHelperInstace.setBoolean(applicationContext, Constants.MessageNotificationKeys.ENABLE_NOTIFICATION, true);

})

firebasemessagingservice:---

override fun onMessageReceived(remoteMessage: RemoteMessage) {

super.onMessageReceived(remoteMessage)

if (PreferenceHelper.prefernceHelperInstace.getBoolean(getApplicationContext(),

Constants.MessageNotificationKeys.ENABLE_NOTIFICATION, true)

) {

Log.d("msg", "onMessageReceived: " + remoteMessage.notification?.body)

val intent = Intent(this, HomeActivity::class.java)

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

val pendingIntent =

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)

val channelId = "Default"

val builder: NotificationCompat.Builder = NotificationCompat.Builder(this, channelId)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle(remoteMessage.getNotification()?.getTitle())

.setContentText(remoteMessage.getNotification()?.getBody()).setAutoCancel(true)

.setContentIntent(pendingIntent)

.setStyle(NotificationCompat.BigTextStyle()

.bigText(remoteMessage.getNotification()?.getBody()))

val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

val channel = NotificationChannel(

channelId,

"Default channel",

NotificationManager.IMPORTANCE_DEFAULT

)

manager!!.createNotificationChannel(channel)

}

manager!!.notify(0, builder.build())

}

else {

Log.e("TAG", "ReactFireBaseMessagingService: Notifications Are Disabled by User");

}

}

preferencehelper:--

class PreferenceHelper private constructor() {

fun setBoolean(appContext: Context?, key: String?, value: Boolean?) {

PreferenceManager.getDefaultSharedPreferences(appContext).edit()

.putBoolean(key, value!!).apply()

}

fun getBoolean(

appContext: Context?, key: String?,

defaultValue: Boolean?

): Boolean {

return PreferenceManager.getDefaultSharedPreferences(appContext)

.getBoolean(key, defaultValue!!)

}

fun getInteger(appContext: Context?, key: String?, defaultValue: Int): Int {

return PreferenceManager.getDefaultSharedPreferences(appContext)

.getInt(key, defaultValue)

}

companion object {

val prefernceHelperInstace = PreferenceHelper()

}

}

using the method of topic(need help ):---------

val sharedPreferences = getSharedPreferences("myname", MODE_PRIVATE)

simpleSwitch.setChecked(sharedPreferences.getBoolean("SWITCH_STATE", false))

simpleSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->

sharedPreferences.edit().putBoolean("SWITCH_STATE", isChecked).commit()

if (isChecked) {

// FirebaseMessaging.getInstance().subscribeToTopic("Partidos")

FirebaseMessaging.getInstance().subscribeToTopic("main_notification");

Toast.makeText(applicationContext, "enabled notification",

Toast.LENGTH_LONG).show()

}

else {

FirebaseMessaging.getInstance().unsubscribeFromTopic("main_notification");

Toast.makeText(applicationContext, "disabled notification",

Toast.LENGTH_LONG).show()

}

})

The problem of this code is it doesn't worked at first(it receives notification at on off both) after switching on off (switching buttons) it works(when on receives notification and off doesn't receive).

解决方案

FirebaseMessagingService runs in the background even when the app's not in the foreground so you won't be able to get the preferences using applicationContext.

You should use Topic messaging - https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

Use this in your switch's change listener:

To enable push notifications -

FirebaseMessaging.getInstance().subscribeToTopic("your_topic");

To disable push notifications -

FirebaseMessaging.getInstance().unsubscribeFromTopic("your_topic");

This way you will notify Firebase that you don't want to get notifications about a particular topic.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值