重构通知栏模块

This commit is contained in:
ZhanGSKen 2025-05-06 17:04:14 +08:00
parent df6633046c
commit 6555346618
3 changed files with 92 additions and 60 deletions

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue May 06 11:34:02 HKT 2025
#Tue May 06 09:02:43 GMT 2025
stageCount=3
libraryProject=
baseVersion=15.0
publishVersion=15.0.2
buildCount=0
buildCount=2
baseBetaVersion=15.0.3

View File

@ -120,7 +120,7 @@ public class NotificationHelper {
PendingIntent pendingIntent = createPendingIntent(intent);
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMPORARY)
Notification notification = new NotificationCompat.Builder(mContext, CHANNEL_ID_FOREGROUND)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
//.setContentTitle(title)

View File

@ -5,16 +5,12 @@ package cc.winboll.studio.timestamp.utils;
* @Date 2025/05/05 21:10
* @Describe TimeStampRemoteViewsUtil
*/
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;
import android.widget.TextView;
import androidx.core.app.NotificationCompat;
import cc.winboll.studio.timestamp.MainActivity;
import cc.winboll.studio.timestamp.R;
import cc.winboll.studio.timestamp.receivers.ButtonClickReceiver;
@ -27,13 +23,16 @@ public class TimeStampRemoteViewsUtil {
static volatile TimeStampRemoteViewsUtil _TimeStampRemoteViewsUtil;
Context mContext;
NotificationHelper mNotificationHelper;
RemoteViews mRemoteViews;
TextView mtvMessage;
Notification mNotification;
Intent mIntentMain;
TimeStampRemoteViewsUtil(Context context) {
mContext = context;
createNotificationChannel();
mNotificationHelper = new NotificationHelper(context);
//createNotificationChannel();
}
public static synchronized TimeStampRemoteViewsUtil getInstance(Context context) {
@ -42,60 +41,93 @@ public class TimeStampRemoteViewsUtil {
}
return _TimeStampRemoteViewsUtil;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "自定义视图通知通道";
String description = "用于展示自定义视图的通知通道";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
// private void createNotificationChannel() {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// CharSequence name = "自定义视图通知通道";
// String description = "用于展示自定义视图的通知通道";
// int importance = NotificationManager.IMPORTANCE_HIGH;
// NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
// channel.setDescription(description);
// NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
// notificationManager.createNotificationChannel(channel);
// }
// }
// public void showNotification(String msg) {
// if (mRemoteViews == null) {
// // 创建 RemoteViews 对象加载布局
// mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_layout);
//
// }
// // 自定义 TextView 的文本
// mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
// // 自定义 TextView 的文本颜色
// mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
// // 这里虽然不能直接设置字体大小但可以通过反射等方式尝试不推荐且有兼容性问题
//
// // 创建点击通知后的意图
// Intent intent = new Intent(mContext, MainActivity.class);
// PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// // 设置通知的点击事件
// mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
//
// // 创建点击按钮后要发送的广播 Intent
// Intent broadcastIntent = new Intent(ButtonClickReceiver.BUTTON_COPYTIMESTAMP_ACTION);
// android.app.PendingIntent pendingIntent = android.app.PendingIntent.getBroadcast(
// mContext,
// 0,
// broadcastIntent,
// android.app.PendingIntent.FLAG_UPDATE_CURRENT
// );
//
// // 为按钮设置点击事件
// mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
//
// // 构建通知
// NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
// .setSmallIcon(android.R.drawable.ic_dialog_info)
// .setContent(mRemoteViews)
// .setPriority(NotificationCompat.PRIORITY_HIGH)
// .setOngoing(true)
// .setAutoCancel(true);
//
// // 显示通知
// NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
// mNotification = builder.build();
// notificationManager.notify(1, mNotification);
// }
public void showNotification(String msg) {
if (mRemoteViews == null) {
// 创建 RemoteViews 对象加载布局
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_layout);
// 自定义 TextView 的文本
mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
// 自定义 TextView 的文本颜色
mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
// 这里虽然不能直接设置字体大小但可以通过反射等方式尝试不推荐且有兼容性问题
// 创建点击通知后的意图
mIntentMain = new Intent(mContext, MainActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, mIntentMain, PendingIntent.FLAG_UPDATE_CURRENT);
// 设置通知的点击事件
mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
// 创建点击按钮后要发送的广播 Intent
Intent broadcastIntent = new Intent(ButtonClickReceiver.BUTTON_COPYTIMESTAMP_ACTION);
android.app.PendingIntent pendingIntent = android.app.PendingIntent.getBroadcast(
mContext,
0,
broadcastIntent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT
);
// 为按钮设置点击事件
mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
}
// 自定义 TextView 的文本
mRemoteViews.setTextViewText(R.id.tv_timestamp, msg);
// 自定义 TextView 的文本颜色
mRemoteViews.setTextColor(R.id.tv_timestamp, mContext.getResources().getColor(R.color.colorAccent, null));
// 这里虽然不能直接设置字体大小但可以通过反射等方式尝试不推荐且有兼容性问题
// 创建点击通知后的意图
Intent intent = new Intent(mContext, MainActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 设置通知的点击事件
mRemoteViews.setOnClickPendingIntent(R.id.tv_timestamp, pendingMainIntent);
// 创建点击按钮后要发送的广播 Intent
Intent broadcastIntent = new Intent(ButtonClickReceiver.BUTTON_COPYTIMESTAMP_ACTION);
android.app.PendingIntent pendingIntent = android.app.PendingIntent.getBroadcast(
mContext,
0,
broadcastIntent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT
);
// 为按钮设置点击事件
mRemoteViews.setOnClickPendingIntent(R.id.btn_copytimestamp, pendingIntent);
// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContent(mRemoteViews)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setAutoCancel(true);
// 显示通知
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
mNotification = builder.build();
notificationManager.notify(1, mNotification);
mNotificationHelper.showCustomForegroundNotification(mIntentMain, mRemoteViews, mRemoteViews);
}
}