通知调试完成,添加旧通知设置清理按钮。
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Mon Mar 31 20:04:51 HKT 2025 | ||||
| #Tue Apr 01 11:52:27 GMT 2025 | ||||
| stageCount=4 | ||||
| libraryProject= | ||||
| baseVersion=15.2 | ||||
| publishVersion=15.2.3 | ||||
| buildCount=0 | ||||
| buildCount=10 | ||||
| baseBetaVersion=15.2.4 | ||||
|   | ||||
| @@ -17,6 +17,8 @@ import cc.winboll.studio.libaes.views.AToolbar; | ||||
| import cc.winboll.studio.mymessagemanager.R; | ||||
| import cc.winboll.studio.mymessagemanager.utils.AppConfigUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.PermissionUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.NotificationHelper; | ||||
| import cc.winboll.studio.libappbase.dialogs.YesNoAlertDialog; | ||||
|  | ||||
| public class AppSettingsActivity extends BaseActivity { | ||||
|  | ||||
| @@ -96,4 +98,18 @@ public class AppSettingsActivity extends BaseActivity { | ||||
|             Toast.makeText(getApplication(), "应用已获得所需权限。", Toast.LENGTH_SHORT).show(); | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     public void onCleanOldChannels(View view) { | ||||
|         YesNoAlertDialog.show(this, "通知设置清理", "是否清理旧的通知设置?", new YesNoAlertDialog.OnDialogResultListener(){ | ||||
|                 @Override | ||||
|                 public void onNo() { | ||||
|                 } | ||||
|  | ||||
|                 @Override | ||||
|                 public void onYes() { | ||||
|                     NotificationHelper notificationHelper = new NotificationHelper(AppSettingsActivity.this); | ||||
|                     notificationHelper.cleanOldChannels(); | ||||
|                 } | ||||
|             }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,6 @@ import cc.winboll.studio.mymessagemanager.services.MainService; | ||||
| import cc.winboll.studio.mymessagemanager.unittest.UnitTestActivity; | ||||
| import cc.winboll.studio.mymessagemanager.utils.AppConfigUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.AppGoToSettingsUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.NotificationUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.PermissionUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.ViewUtil; | ||||
| @@ -129,12 +128,7 @@ public class MainActivity extends BaseActivity { | ||||
|         mToolbar = findViewById(R.id.activitymainASupportToolbar1); | ||||
|         mToolbar.setSubtitle(getString(R.string.activity_name_main)); | ||||
|         setSupportActionBar(mToolbar); | ||||
|  | ||||
|         // 创建通知频道 | ||||
|         NotificationUtil nu = new NotificationUtil(); | ||||
|         nu.createServiceNotificationChannel(MainActivity.this); | ||||
|         nu.createSMSNotificationChannel(MainActivity.this); | ||||
|  | ||||
|          | ||||
|         boolean isEnableService = mAppConfigUtil.mAppConfigBean.isEnableService(); | ||||
|         msvEnableService = findViewById(R.id.activitymainSwitchView1); | ||||
|         msvEnableService.setChecked(isEnableService); | ||||
|   | ||||
| @@ -18,7 +18,6 @@ import cc.winboll.studio.mymessagemanager.R; | ||||
| import cc.winboll.studio.mymessagemanager.activitys.TTSPlayRuleActivity; | ||||
| import cc.winboll.studio.mymessagemanager.beans.SMSBean; | ||||
| import cc.winboll.studio.mymessagemanager.dialogs.YesNoAlertDialog; | ||||
| import cc.winboll.studio.mymessagemanager.utils.NotificationUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSReceiveRuleUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSRecycleUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSUtil; | ||||
| @@ -28,6 +27,7 @@ import cc.winboll.studio.mymessagemanager.views.SMSView; | ||||
| import com.hjq.toast.ToastUtils; | ||||
| import java.util.ArrayList; | ||||
| import cc.winboll.studio.mymessagemanager.beans.SMSAcceptRuleBean; | ||||
| import cc.winboll.studio.mymessagemanager.utils.NotificationHelper; | ||||
|  | ||||
| public class SMSArrayAdapter extends BaseAdapter { | ||||
|  | ||||
| @@ -54,7 +54,8 @@ public class SMSArrayAdapter extends BaseAdapter { | ||||
|  | ||||
|     public void cancelMessageNotification() { | ||||
|         for (SMSBean bean : mData) { | ||||
|             NotificationUtil.cancelNotification(mContext, bean.getId()); | ||||
|             NotificationHelper notificationHelper = new NotificationHelper(mContext); | ||||
|             notificationHelper.cancelNotification(bean.getId()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -14,6 +14,7 @@ import cc.winboll.studio.mymessagemanager.utils.SMSReceiveRuleUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSRecycleUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.SMSUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.TTSPlayRuleUtil; | ||||
| import cc.winboll.studio.mymessagemanager.utils.NotificationHelper; | ||||
|  | ||||
| public class SMSRecevier extends BroadcastReceiver { | ||||
|  | ||||
| @@ -45,8 +46,8 @@ public class SMSRecevier extends BroadcastReceiver { | ||||
|             if (checkIsSMSOK(context, szSmsBody, szSmsAddress)) { | ||||
|                 int nResultId = SMSUtil.saveReceiveSms(context, szSmsAddress, szSmsBody, "0", System.currentTimeMillis(), "inbox"); | ||||
|                 if (nResultId >= 0) { | ||||
|                     NotificationUtil nu = new NotificationUtil(); | ||||
|                     nu.sendSMSReceivedMessage(context, nResultId, szSmsAddress, szSmsBody); | ||||
|                     NotificationHelper notificationHelper = new NotificationHelper(context); | ||||
|                     notificationHelper.sendSMSReceivedMessage(nResultId, szSmsAddress, szSmsBody); | ||||
|                     LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(SMSActivity.ACTION_NOTIFY_SMS_CHANGED)); | ||||
|                     LogUtils.d(TAG, "<" + szSmsAddress + "> : ( " + szSmsBody + " ) [SAVED]"); | ||||
|                     if (isEnableTTS) { | ||||
|   | ||||
| @@ -16,8 +16,12 @@ import android.os.Build; | ||||
| import android.widget.RemoteViews; | ||||
| import androidx.annotation.RequiresApi; | ||||
| import androidx.core.app.NotificationCompat; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import cc.winboll.studio.mymessagemanager.R; | ||||
| import cc.winboll.studio.mymessagemanager.activitys.SMSActivity; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| public class NotificationHelper { | ||||
|     public static final String TAG = "NotificationHelper"; | ||||
| @@ -35,9 +39,24 @@ public class NotificationHelper { | ||||
|     private final Context mContext; | ||||
|     private final NotificationManager mNotificationManager; | ||||
|  | ||||
|     // 示例:维护当前使用的渠道ID列表 | ||||
|     // 键:渠道ID,值:渠道重要性级别 | ||||
|     Map<String, Integer> activeChannelConfigs = new HashMap<>(); | ||||
|  | ||||
|     public NotificationHelper(Context context) { | ||||
|         mContext = context; | ||||
|         mNotificationManager = context.getSystemService(NotificationManager.class); | ||||
|  | ||||
|         // 初始化配置 | ||||
|         activeChannelConfigs.put( | ||||
|             CHANNEL_ID_FOREGROUND, | ||||
|             NotificationManager.IMPORTANCE_HIGH | ||||
|         ); | ||||
|         activeChannelConfigs.put( | ||||
|             CHANNEL_ID_TEMPORARY, | ||||
|             NotificationManager.IMPORTANCE_DEFAULT | ||||
|         ); | ||||
|  | ||||
|         createNotificationChannels(); | ||||
|     } | ||||
|  | ||||
| @@ -97,6 +116,11 @@ public class NotificationHelper { | ||||
|  | ||||
|     // 显示临时通知(自动消失) | ||||
|     public void showTemporaryNotification(Intent intent, String title, String content) { | ||||
|         showTemporaryNotification(intent, TEMPORARY_NOTIFICATION_ID, title, content); | ||||
|     } | ||||
|  | ||||
|     // 显示临时通知(自动消失) | ||||
|     public void showTemporaryNotification(Intent intent, int notificationID, String title, String content) { | ||||
|         PendingIntent pendingIntent = createPendingIntent(intent); | ||||
|  | ||||
|         Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMPORARY) | ||||
| @@ -110,7 +134,7 @@ public class NotificationHelper { | ||||
|             .setVibrate(new long[]{100, 200, 300, 400}) | ||||
|             .build(); | ||||
|  | ||||
|         mNotificationManager.notify(TEMPORARY_NOTIFICATION_ID, notification); | ||||
|         mNotificationManager.notify(notificationID, notification); | ||||
|     } | ||||
|  | ||||
|     // 创建自定义布局通知(可扩展) | ||||
| @@ -134,6 +158,11 @@ public class NotificationHelper { | ||||
|         mNotificationManager.cancelAll(); | ||||
|     } | ||||
|  | ||||
|     // 取消指定通知 | ||||
|     public void cancelNotification(int notificationID) { | ||||
|         mNotificationManager.cancel(notificationID); | ||||
|     } | ||||
|  | ||||
|     // 创建PendingIntent(兼容不同API版本) | ||||
|     private PendingIntent createPendingIntent(Intent intent) { | ||||
|         int flags = PendingIntent.FLAG_UPDATE_CURRENT; | ||||
| @@ -147,12 +176,26 @@ public class NotificationHelper { | ||||
|             flags | ||||
|         ); | ||||
|     } | ||||
|      | ||||
|     public void sendSMSReceivedMessage(int nResultId, String szPhone, String szBody) { | ||||
|  | ||||
|     public void sendSMSReceivedMessage(int notificationID, String szPhone, String szBody) { | ||||
|         Intent intent = new Intent(mContext, SMSActivity.class); | ||||
| 		intent.putExtra(SMSActivity.EXTRA_PHONE, szPhone); | ||||
|         String szTitle = mContext.getString(R.string.text_smsfrom)  + "<" + szPhone + ">"; | ||||
|         String szContent = "[ " + szBody + " ]"; | ||||
|         showTemporaryNotification(intent, szTitle, szContent); | ||||
|         showTemporaryNotification(intent, notificationID, szTitle, szContent); | ||||
|     } | ||||
|  | ||||
|     public void cleanOldChannels() { | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||||
|             List<NotificationChannel> allChannels = mNotificationManager.getNotificationChannels(); | ||||
|             for (NotificationChannel channel : allChannels) { | ||||
|                 LogUtils.d(TAG, "Clean channel : " + channel.getId()); | ||||
|                 if (!activeChannelConfigs.containsKey(channel.getId())) { | ||||
|                     // 安全删除渠道 | ||||
|                     mNotificationManager.deleteNotificationChannel(channel.getId()); | ||||
|                     LogUtils.d(TAG, String.format("Deleted Channel %s", channel.getId())); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -213,6 +213,12 @@ | ||||
| 						android:text="检查应用权限" | ||||
| 						android:onClick="onCheckAndGetAppPermission"/> | ||||
|  | ||||
|                     <Button | ||||
|                         android:layout_width="wrap_content" | ||||
|                         android:layout_height="wrap_content" | ||||
|                         android:text="清理通知设置" | ||||
|                         android:onClick="onCleanOldChannels"/> | ||||
|                      | ||||
| 				</LinearLayout> | ||||
|  | ||||
| 			</LinearLayout> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen