How to turn off App notification from inside the app
Asked Answered
G

3

6

How do you turn off Notification programmatically from a settings options inside an app?

The approach of creating the Notification channel inside an if statement in a Preference change listener seems to be doing the job. I'm concerned that the I'm creating multiple Notification channels now.

Preference.xml

<PreferenceCategory
    app:key="notification_category"
    app:title="Notification"
    <SwitchPreferenceCompat
         app:key="notification_state"
         app:title="Enable notification message"/>
</PrefenceCategory>

MainActivity.class

boolean mNotificationState;
Notification mNotification;

SharedPreferences.OnSharedPreferenceChangeListener listener;


@Override
protected void onCreate(Bundle saveInstanceState) {
    ...
    // Create notification object.
    mNotification = Notification(this);

    // Prefernce Listener
    listner = (sharedPrefences, s) -> {
        mNotificationState = SharedPreferences.getBoolean("notification_state", true);
    if(mNotificationState) {
        // concerned that a memory leak migh occur.
        notification.createNotificationChannel();
    } else {
        notification.getNotificationManager().cancelAll();
    }
}

/**
 * registerOnSharedPreferenceChangeListener
 */
@Override
public void onResume() {   
    super.onResume();
PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).registerOnSharedPreferenceChangeListener(listener);
} 

/*
 * unregisterOnSharedPreferenceChangeListener.
 */
@Override
public void onDestroy() {
    PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).unregisterOnSharedPreferenceChangeListener(listener);
    super.onDestroy(); 
}   

Notification.class, contains a NotificationChannel method, notfication method, and getNotificationManager (gettter).

private Context mContext;

// Constructor
public Notification(Context context) {
    this.context = context;
}

// Notification channel. 
public void createNotificationChannel() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "notification_ID";
        String description = "My Notificatin";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
    }
    // Set NotificationChannel ch = new NotificationChannel(NOTIFICATION_ID, name, importance);
    ch.setDescription(description);

    // Register the channel.
    notificationManager = context.getSystemService(NotificationManager.class);

    notificationManager.createNotificationChannel(ch);
}

public NotificationManager getNotificationManager() {
    return notificationManager;
}
Gleesome answered 22/9, 2019 at 7:54 Comment(5)
How about saving the toggle value is SharedPreferencs and when the notification receives then show it to the user according to the toggle value savedCimbura
Do you use FCM?Invalidity
Im not using FCM, notification are coming from the app not a server.Gleesome
Do you use java or kotlin?Invalidity
@NatigBabayev I'm using JavaGleesome
B
9

If the notif not push messages type, then :

  1. Disable all currently shown notification and toggle off sharedPrefs value

checkbox.setOnCheckedChangeListener { _, isChecked ->

        if (isChecked) {

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, true)

        } else {

            NotificationManagerCompat.from(this).cancelAll()

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, false)

        }

    }
  1. Check MySharedPrefs.NOTIFICATION_ENABLED state at part of the code where you actually build and show the notification(eg. BroadcastReceiver)
if (mSharedPrefs.getValueBoolean(MySharedPrefs.NOTIFICATION_ENABLED)) {
    val builder = NotificationCompat.Builder(context, Constants.CHANNEL_ID)

     NotificationManagerCompat.from(context).notify(Constants.NOTIFICATION_ID, builder.build())
}

Bother answered 26/9, 2019 at 1:47 Comment(1)
I created my code based on your suggestion, could you please have a look at.Gleesome
N
1

Unfortunately, Android does not have an API to disable Notifications. However, you can delete the GCM/FCM token from server, thus achieving the objective.

Nickels answered 25/9, 2019 at 4:21 Comment(1)
how do they accomplish in the stack overflow app then?Gleesome
J
0

You can save the user's choice in shared preferences. Then check for its value and show the notification if its value is true else don't show it.

Jacky answered 26/9, 2019 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.