gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:01:17 28 4
gpt4 key购买 nike

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

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

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;
}

最佳答案

如果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)

}

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

关于android - 如何从应用程序内部关闭应用程序通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58047177/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com