From de23c355859cf9e0e1a4bd60197fbd9dc4988b8e Mon Sep 17 00:00:00 2001 From: taohebin Date: Thu, 30 Nov 2023 15:52:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20uni-progress-notification=20createNotifi?= =?UTF-8?q?cationProgress=E5=A2=9E=E5=8A=A0onClick?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-progress-notification/changelog.md | 5 ++ .../uni-progress-notification/package.json | 2 +- .../uni-progress-notification/readme.md | 9 +- .../app-android/TransparentActivity.uts | 83 +++++++++++-------- .../utssdk/app-android/index.uts | 51 +++++++----- .../utssdk/interface.uts | 16 +++- 6 files changed, 103 insertions(+), 63 deletions(-) diff --git a/uni_modules/uni-progress-notification/changelog.md b/uni_modules/uni-progress-notification/changelog.md index 9e05173..1066eb5 100644 --- a/uni_modules/uni-progress-notification/changelog.md +++ b/uni_modules/uni-progress-notification/changelog.md @@ -1,3 +1,8 @@ +## 1.0.4(2023-11-30) +1. createNotificationProgress增加`onClick`回调 +2.修复在小米部分系统上,通知消息会归类于不重要通知的bug +## 1.0.3(2023-11-28) +更新截图 ## 1.0.2(2023-11-28) 修改资源的包名 ## 1.0.1(2023-11-28) diff --git a/uni_modules/uni-progress-notification/package.json b/uni_modules/uni-progress-notification/package.json index c2db422..e4bae82 100644 --- a/uni_modules/uni-progress-notification/package.json +++ b/uni_modules/uni-progress-notification/package.json @@ -1,7 +1,7 @@ { "id": "uni-progress-notification", "displayName": "uni-progress-notification", - "version": "1.0.2", + "version": "1.0.4", "description": "uni-progress-notification", "keywords": [ "uni-progress-notification" diff --git a/uni_modules/uni-progress-notification/readme.md b/uni_modules/uni-progress-notification/readme.md index fd0e987..161bf99 100644 --- a/uni_modules/uni-progress-notification/readme.md +++ b/uni_modules/uni-progress-notification/readme.md @@ -26,7 +26,12 @@ export type CreateNotificationProgressOptions = { /** * 进度 */ - progress : number + progress : number, + /** + * 点击通知消息回调 + * @defaultValue null + */ + onClick? : (() => void) | null } ``` @@ -51,7 +56,7 @@ export type FinishNotificationProgressOptions = { /** * 点击通知消息回调 */ - callback : () => void + onClick : () => void } ``` diff --git a/uni_modules/uni-progress-notification/utssdk/app-android/TransparentActivity.uts b/uni_modules/uni-progress-notification/utssdk/app-android/TransparentActivity.uts index 025b502..0368df2 100644 --- a/uni_modules/uni-progress-notification/utssdk/app-android/TransparentActivity.uts +++ b/uni_modules/uni-progress-notification/utssdk/app-android/TransparentActivity.uts @@ -4,45 +4,58 @@ import Build from 'android.os.Build'; import View from 'android.view.View'; import Color from 'android.graphics.Color'; import WindowManager from 'android.view.WindowManager'; -import { globalNotificationProgressFinishCallBack } from './index.uts'; -import { ACTION_DOWNLOAD_FINISH } from "./constant.uts" +import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './index.uts'; +import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" export class TransparentActivity extends Activity { - constructor() { - super() - } + constructor() { + super() + } - override onCreate(savedInstanceState : Bundle | null) { - super.onCreate(savedInstanceState) - this.fullScreen(this) - const action = this.getIntent().getAction() - if (action == ACTION_DOWNLOAD_FINISH) { - globalNotificationProgressFinishCallBack() - this.overridePendingTransition(0, 0) - } + override onCreate(savedInstanceState : Bundle | null) { + super.onCreate(savedInstanceState) + this.fullScreen(this) + const action = this.getIntent().getAction() + if (action == ACTION_DOWNLOAD_FINISH) { + setTimeout(() => { + globalNotificationProgressFinishCallBack() + globalNotificationProgressFinishCallBack = () => { } + }, 100) + this.overridePendingTransition(0, 0) + } - setTimeout(() => { - this.finish() - }, 20) - } + if (action == ACTION_DOWNLOAD_PROGRESS) { + setTimeout(() => { + globalNotificationProgressCallBack?.() + globalNotificationProgressCallBack = () => { } + }, 100) + this.overridePendingTransition(0, 0) + } - private fullScreen(activity : Activity) { - if (Build.VERSION.SDK_INT >= 19) { - if (Build.VERSION.SDK_INT >= 21) { - const window = activity.getWindow(); - const decorView = window.getDecorView(); - const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; - decorView.setSystemUiVisibility(option); - window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); - window.setStatusBarColor(Color.TRANSPARENT); - } else { - const window = activity.getWindow(); - const attributes = window.getAttributes(); - const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; - attributes.flags |= flagTranslucentStatus; - window.setAttributes(attributes); - } - } - } + setTimeout(() => { + this.finish() + }, 20) + } + + + @Suppress("DEPRECATION") + private fullScreen(activity : Activity) { + if (Build.VERSION.SDK_INT >= 19) { + if (Build.VERSION.SDK_INT >= 21) { + const window = activity.getWindow(); + const decorView = window.getDecorView(); + const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; + decorView.setSystemUiVisibility(option); + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); + window.setStatusBarColor(Color.TRANSPARENT); + } else { + const window = activity.getWindow(); + const attributes = window.getAttributes(); + const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; + attributes.flags |= flagTranslucentStatus; + window.setAttributes(attributes); + } + } + } } \ No newline at end of file diff --git a/uni_modules/uni-progress-notification/utssdk/app-android/index.uts b/uni_modules/uni-progress-notification/utssdk/app-android/index.uts index 48c3f5e..a04b7e0 100644 --- a/uni_modules/uni-progress-notification/utssdk/app-android/index.uts +++ b/uni_modules/uni-progress-notification/utssdk/app-android/index.uts @@ -6,9 +6,9 @@ import Notification from 'android.app.Notification'; import Intent from 'android.content.Intent'; import ComponentName from 'android.content.ComponentName'; import PendingIntent from 'android.app.PendingIntent'; -import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts'; -import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS} from "./constant.uts" - +import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts'; +import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" + export { TransparentActivity } from './TransparentActivity.uts'; @@ -26,11 +26,17 @@ let histroyProgress = 0 let isProgress = false +export let globalNotificationProgressCallBack : (() => void) | null = () => { } export let globalNotificationProgressFinishCallBack = () => { } -export function createNotificationProgress(options : CreateNotificationProgressOptions) : void { - const { content, progress } = options +export function createNotificationProgress(options : CreateNotificationProgressOptions) : void { + const { content, progress, onClick } = options + if (progress == 100) { + clearTimeout(timeId) + const context = UTSAndroid.getAppContext() as Context + realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick) + reset() return } @@ -39,13 +45,13 @@ export function createNotificationProgress(options : CreateNotificationProgressO return } - const context = UTSAndroid.getAppContext() as Context + const context = UTSAndroid.getAppContext() as Context if (!isProgress) { - realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress) + realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) isProgress = true } else { - timeId = setTimeout(() => { - realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress) + timeId = setTimeout(() => { + realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) timeId = -1 }, 1000) } @@ -60,12 +66,13 @@ export function cancelNotificationProgress() : void { } -function realCreateNotificationProgress(title : string, content : string, progress : number) : void { +function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void { + globalNotificationProgressCallBack = cb const context = UTSAndroid.getAppContext() as Context const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager createDownloadChannel(notificationManager) const builder = createNotificationBuilder(context) - builder.setProgress(100, progress.toInt(), false) + builder.setProgress(100, progress.toInt(), false) builder.setContentTitle(title) builder.setContentText(content) builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS)); @@ -73,16 +80,17 @@ function realCreateNotificationProgress(title : string, content : string, progre } -export function finishNotificationProgress(options : FinishNotificationProgressOptions) { - globalNotificationProgressFinishCallBack = options.callback +export function finishNotificationProgress(options : FinishNotificationProgressOptions) { + globalNotificationProgressFinishCallBack = options.onClick const context = UTSAndroid.getAppContext() as Context const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager createDownloadChannel(notificationManager) const builder = createNotificationBuilder(context) - builder.setProgress(0, 0, false) + builder.setProgress(0, 0, false) builder.setContentTitle(options.title ?? getAppName(context)) builder.setContentText(options.content) - builder.setOngoing(false) + //小米rom setOngoing未false的时候,会被通知管理器归为不重要通知 + // builder.setOngoing(false) builder.setAutoCancel(true); builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH)); notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) @@ -122,7 +130,7 @@ function createDownloadChannel(notificationManager : NotificationManager) { notificationManager.createNotificationChannel(channel) } } - +@Suppress("DEPRECATION") function createNotificationBuilder(context : Context) : Notification.Builder { if (notificationBuilder == null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -130,13 +138,14 @@ function createNotificationBuilder(context : Context) : Notification.Builder { } else { notificationBuilder = new Notification.Builder(context) } - notificationBuilder!!.setSmallIcon(context.getApplicationInfo().icon) - notificationBuilder!!.setOngoing(true) - notificationBuilder!!.setSound(null) + notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon) + notificationBuilder!.setOngoing(true) + notificationBuilder!.setSound(null) } - return notificationBuilder!! + return notificationBuilder! } +@Suppress("DEPRECATION") function getAppName(context : Context) : string { let appName = "" try { @@ -147,4 +156,4 @@ function getAppName(context : Context) : string { e.printStackTrace() } return appName -} +} \ No newline at end of file diff --git a/uni_modules/uni-progress-notification/utssdk/interface.uts b/uni_modules/uni-progress-notification/utssdk/interface.uts index 0538f13..74841bf 100644 --- a/uni_modules/uni-progress-notification/utssdk/interface.uts +++ b/uni_modules/uni-progress-notification/utssdk/interface.uts @@ -24,7 +24,10 @@ export type uni = { uni.createNotificationProgress({ title: "正在下载升级包", content: "进度 50%", - progress: 50 + progress: 50, + onClick:()=>{ + console.log("正在下载"); + } } ``` */ @@ -54,7 +57,7 @@ export type uni = { uni.finishNotificationProgress({ title: "安装升级包", content: "下载完成。", - callback: () => { + onClick: () => { uni.installApk({ filePath: e.tempFilePath, complete(res) { @@ -107,7 +110,12 @@ export type CreateNotificationProgressOptions = { /** * 进度 */ - progress : number + progress : number, + /** + * 点击通知消息回调 + * @defaultValue null + */ + onClick? : (() => void) | null } @@ -124,7 +132,7 @@ export type FinishNotificationProgressOptions = { /** * 点击通知消息回调 */ - callback : () => void + onClick : () => void } -- GitLab