如何从应用程序内部关闭应用程序通知

Sha*_*awn 2 android android-preferences android-notifications

如何从应用程序内的设置选项以编程方式关闭通知?

在 Preference 更改侦听器的 if 语句中创建 Notification 通道的方法似乎正在完成这项工作。我担心我现在正在创建多个通知渠道。

首选项.xml

<PreferenceCategory
    app:key="notification_category"
    app:title="Notification"
    <SwitchPreferenceCompat
         app:key="notification_state"
         app:title="Enable notification message"/>
</PrefenceCategory>
Run Code Online (Sandbox Code Playgroud)

主Activity.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(); 
}   
Run Code Online (Sandbox Code Playgroud)

Notification.class,包含 NotificationChannel 方法、notfication 方法和 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;
}
Run Code Online (Sandbox Code Playgroud)

nfl*_*l-x 6

如果 notif 不是推送消息类型,则:

  1. 禁用所有当前显示的通知并关闭 sharedPrefs 值

checkbox.setOnCheckedChangeListener { _, isChecked ->

        if (isChecked) {

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, true)

        } else {

            NotificationManagerCompat.from(this).cancelAll()

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, false)

        }

    }
Run Code Online (Sandbox Code Playgroud)
  1. 检查MySharedPrefs.NOTIFICATION_ENABLED您实际构建和显示通知的代码部分的状态(例如 BroadcastReceiver)
if (mSharedPrefs.getValueBoolean(MySharedPrefs.NOTIFICATION_ENABLED)) {
    val builder = NotificationCompat.Builder(context, Constants.CHANNEL_ID)

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

Run Code Online (Sandbox Code Playgroud)