skip to Main Content

Enable and disable the firebase notification using switch buttons.? i used php code for backend to take the fcm token and place it in the database(phpmyadmin).Then i generate a push notification to the app. The notification coming correctly but i can’t stop it. i want to enable and disable the notification with switch buttons.

public class FcmMsgService  extends FirebaseMessagingService {
    private static final String NOTIFICATION_CHANNEL_ID1 = "channel_id" ;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    {

        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        String click_action = remoteMessage.getNotification().getClickAction();
        Intent intent = new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID1);
        builder.setContentTitle(title);
        builder.setContentText(message);
        builder.setSmallIcon(R.drawable.ic_notifications);
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
        // super.onMessageReceived(remoteMessage);
    }
}

Mainactiviy

public class MainActivity extends AppCompatActivity {

    Switch switchBtn;
    SharedPreferences.Editor prefEditor;
    SharedPreferences prefs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn= findViewById(R.id.send_token);
        prefEditor= PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        switchBtn= findViewById(R.id.switch2);

            switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    if (switchBtn.isChecked()) 
                          {

          // when i user enable button the notification can allowed to come
                        prefEditor.putString("checked","yes");
                        prefEditor.apply();

                          }
                    else
                     {
         // when i user disable button the notification cannot allowed to come
                        //how to Stop the firebase notification?????
                        prefEditor.putString("checked","no");
                        prefEditor.apply();
                   }}
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/RY5yp.png

2

Answers


  1. For enabling notification via a topic:

    FirebaseMessaging.getInstance()
                .subscribeToTopic(yourTopic);
    

    Then for disabling:

    FirebaseMessaging.getInstance().unsubscribeFromTopic(yourTopic);
    

    In your code:

    if (switchBtn.isChecked()) {
      // Subscribe here
      FirebaseMessaging.getInstance().subscribeToTopic(yourTopic);
      prefEditor.putString("checked","yes");
      prefEditor.apply();
    }
    else {
      // Subscribe here
      FirebaseMessaging.getInstance().unsubscribeFromTopic(yourTopic);
      prefEditor.putString("checked","no");
      prefEditor.apply();
    }
    

    Also, as this function returns a task, you can do:

    TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopic(
        registrationTokens, topic);
    // See the TopicManagementResponse reference documentation
    // for the contents of response.
    System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");
    
    Login or Signup to reply.
  2. Maybe try unsubscribe from each topic?

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

    Also, this code can be simplified:

    switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        prefEditor.putBoolean("checked", switchBtn.isChecked()).apply();
                }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search