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

How to turn off App notification from inside the app

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

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

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,包含一个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;
}

不幸的是,Android 没有 API 来禁用通知。 但是,您可以从服务器中删除 GCM/FCM 令牌,从而实现 objective.

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

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

checkbox.setOnCheckedChangeListener { _, isChecked ->

        if (isChecked) {

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, true)

        } else {

            NotificationManagerCompat.from(this).cancelAll()

            mSharedPrefs.save(MySharedPrefs.NOTIFICATION_ENABLED, false)

        }

    }
  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())
}

您可以在共享首选项中保存用户的选择。然后检查它的值,如果它的值是 true 则显示通知,否则不显示它。