title: push notification enable and disable by using switch
body: <p>I'm using firebase push notification(FCM).. and I want to enable and disable notifications using a switch button.</p>
  <p>For that I have shared preferences to enable and disable notifications but it seems my logic is not at all working.</p>
  <p>It doesn't make any difference if the switch is turn on or off. I am still receiving notifications.</p>
  <p>I need help thanks.</p>
  <p>activity:--</p>
  <pre><code>  val sharedPreferences = getSharedPreferences(&quot;myname&quot;, MODE_PRIVATE)
  
      simpleSwitch.setChecked(sharedPreferences.getBoolean(&quot;SWITCH_PARTIDOS_STATE&quot;, false))
  
      simpleSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked -&gt;
          sharedPreferences.edit().putBoolean(&quot;SWITCH_PARTIDOS_STATE&quot;, isChecked).commit()
          if (isChecked) {
             // FirebaseMessaging.getInstance().subscribeToTopic(&quot;Partidos&quot;)
  
              Toast.makeText(applicationContext, &quot;Activado Correctamente&quot;,
                  Toast.LENGTH_LONG).show()
          } else {
            //  FirebaseMessaging.getInstance().unsubscribeFromTopic(&quot;Partidos&quot;)
              Toast.makeText(applicationContext, &quot;Desactivado Correctamente&quot;,
                  Toast.LENGTH_LONG).show()
          }
          PreferenceHelper.prefernceHelperInstace.setBoolean(applicationContext, Constants.MessageNotificationKeys.ENABLE_NOTIFICATION, true);
  
      })
  </code></pre>
  <p>firebasemessagingservice:---</p>
  <pre><code>  override fun onMessageReceived(remoteMessage: RemoteMessage) {
      super.onMessageReceived(remoteMessage)
      if (PreferenceHelper.prefernceHelperInstace.getBoolean(getApplicationContext(),
              Constants.MessageNotificationKeys.ENABLE_NOTIFICATION, true)
      ) {
          Log.d(&quot;msg&quot;, &quot;onMessageReceived: &quot; + 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 = &quot;Default&quot;
          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 &gt;= Build.VERSION_CODES.O) {
              val channel = NotificationChannel(
                  channelId,
                  &quot;Default channel&quot;,
                  NotificationManager.IMPORTANCE_DEFAULT
              )
              manager!!.createNotificationChannel(channel)
          }
          manager!!.notify(0, builder.build())
      }
      else {
          Log.e(&quot;TAG&quot;, &quot;ReactFireBaseMessagingService: Notifications Are Disabled by User&quot;);
  
      }
  
  }
  </code></pre>
  <p>preferencehelper:--</p>
  <pre><code>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()
  }
  </code></pre>
  <p>}</p>
  <p>using the method of topic(need help ):---------</p>
  <pre><code>     val sharedPreferences = getSharedPreferences(&quot;myname&quot;, MODE_PRIVATE)
  
      simpleSwitch.setChecked(sharedPreferences.getBoolean(&quot;SWITCH_STATE&quot;, false))
  
      simpleSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked -&gt;
          sharedPreferences.edit().putBoolean(&quot;SWITCH_STATE&quot;, isChecked).commit()
          if (isChecked) {
              // FirebaseMessaging.getInstance().subscribeToTopic(&quot;Partidos&quot;)
    FirebaseMessaging.getInstance().subscribeToTopic(&quot;main_notification&quot;);
           
  
              Toast.makeText(applicationContext, &quot;enabled notification&quot;,
                  Toast.LENGTH_LONG).show()
          }
          else {
              FirebaseMessaging.getInstance().unsubscribeFromTopic(&quot;main_notification&quot;);
              Toast.makeText(applicationContext, &quot;disabled notification&quot;,
                  Toast.LENGTH_LONG).show()
          }
  
      })
  </code></pre>
  <p>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).</p>