提交 f7983bbb 编写于 作者: W WOSHIMAHAIFENG

Merge branch 'dev' of https://gitcode.net/dcloud/hello-uni-app-x into dev-payment

## 1.0.9(2024-02-29)
去除代码过时警告
## 1.0.8(2023-12-21) ## 1.0.8(2023-12-21)
去除app-ios目录 去除app-ios目录
## 1.0.7(2023-12-11) ## 1.0.7(2023-12-11)
......
{ {
"id": "uts-progressNotification", "id": "uts-progressNotification",
"displayName": "uts-progressNotification", "displayName": "uts-progressNotification",
"version": "1.0.8", "version": "1.0.9",
"description": "uts-progressNotification", "description": "uts-progressNotification",
"keywords": [ "keywords": [
"uts-progressNotification" "uts-progressNotification"
......
...@@ -17,25 +17,25 @@ Android平台创建显示进度的通知栏消息 ...@@ -17,25 +17,25 @@ Android平台创建显示进度的通知栏消息
参数说明 参数说明
``` ```
export type CreateNotificationProgressOptions = { export type CreateNotificationProgressOptions = {
/** /**
* 通知标题 * 通知标题
* @defaultValue 应用名称 * @defaultValue 应用名称
*/ */
title ?: string | null title ?: string | null
/** /**
* 通知内容 * 通知内容
*/ */
content : string, content : string,
/** /**
* 进度 * 进度
*/ */
progress : number, progress : number,
/** /**
* 点击通知消息回调 * 点击通知消息回调
* @defaultValue null * @defaultValue null
*/ */
onClick? : (() => void) | null onClick? : (() => void) | null
} }
``` ```
...@@ -47,20 +47,20 @@ export type CreateNotificationProgressOptions = { ...@@ -47,20 +47,20 @@ export type CreateNotificationProgressOptions = {
``` ```
export type FinishNotificationProgressOptions = { export type FinishNotificationProgressOptions = {
/** /**
* 通知标题 * 通知标题
* @defaultValue 应用名称 * @defaultValue 应用名称
*/ */
title ?: string | null title ?: string | null
/** /**
* 通知内容 * 通知内容
*/ */
content : string, content : string,
/** /**
* 点击通知消息回调 * 点击通知消息回调
*/ */
onClick : () => void onClick : () => void
} }
``` ```
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="uts.sdk.modules.utsProgressNotification"> package="uts.sdk.modules.utsProgressNotification">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application> <application>
<activity android:name="uts.sdk.modules.utsProgressNotification.TransparentActivity" <activity android:name="uts.sdk.modules.utsProgressNotification.TransparentActivity"
android:theme="@style/DCNotificationProgressTranslucentTheme" android:hardwareAccelerated="true" android:theme="@style/DCNotificationProgressTranslucentTheme" android:hardwareAccelerated="true"
android:screenOrientation="user" android:exported="true"> android:screenOrientation="user" android:exported="true">
</activity> </activity>
</application> </application>
</manifest> </manifest>
import Activity from "android.app.Activity"; import Activity from "android.app.Activity";
import Bundle from 'android.os.Bundle'; import Bundle from 'android.os.Bundle';
import Build from 'android.os.Build'; import Build from 'android.os.Build';
import View from 'android.view.View'; import View from 'android.view.View';
import Color from 'android.graphics.Color'; import Color from 'android.graphics.Color';
import WindowManager from 'android.view.WindowManager'; import WindowManager from 'android.view.WindowManager';
import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './index.uts'; import { globalNotificationProgressFinishCallBack, globalNotificationProgressCallBack } from './index.uts';
import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts"
export class TransparentActivity extends Activity { export class TransparentActivity extends Activity {
constructor() { constructor() {
super() super()
} }
override onCreate(savedInstanceState : Bundle | null) { @Suppress("DEPRECATION")
super.onCreate(savedInstanceState) override onCreate(savedInstanceState : Bundle | null) {
super.onCreate(savedInstanceState)
this.fullScreen(this) this.fullScreen(this)
const action = this.getIntent().getAction() const action = this.getIntent().getAction()
if (action == ACTION_DOWNLOAD_FINISH) { if (action == ACTION_DOWNLOAD_FINISH) {
setTimeout(() => { setTimeout(() => {
globalNotificationProgressFinishCallBack() globalNotificationProgressFinishCallBack()
globalNotificationProgressFinishCallBack = () => { } globalNotificationProgressFinishCallBack = () => { }
}, 100) }, 100)
this.overridePendingTransition(0, 0) this.overridePendingTransition(0, 0)
} }
if (action == ACTION_DOWNLOAD_PROGRESS) { if (action == ACTION_DOWNLOAD_PROGRESS) {
setTimeout(() => { setTimeout(() => {
globalNotificationProgressCallBack?.() globalNotificationProgressCallBack?.()
globalNotificationProgressCallBack = () => { } globalNotificationProgressCallBack = () => { }
}, 100) }, 100)
this.overridePendingTransition(0, 0) this.overridePendingTransition(0, 0)
} }
setTimeout(() => { setTimeout(() => {
this.finish() this.finish()
}, 20) }, 20)
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fullScreen(activity : Activity) { private fullScreen(activity : Activity) {
if (Build.VERSION.SDK_INT >= 19) { if (Build.VERSION.SDK_INT >= 19) {
if (Build.VERSION.SDK_INT >= 21) { if (Build.VERSION.SDK_INT >= 21) {
const window = activity.getWindow(); const window = activity.getWindow();
const decorView = window.getDecorView(); const decorView = window.getDecorView();
const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; const option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option); decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT); window.setStatusBarColor(Color.TRANSPARENT);
} else { } else {
const window = activity.getWindow(); const window = activity.getWindow();
const attributes = window.getAttributes(); const attributes = window.getAttributes();
const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; const flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
attributes.flags |= flagTranslucentStatus; attributes.flags |= flagTranslucentStatus;
window.setAttributes(attributes); window.setAttributes(attributes);
} }
} }
} }
} }
\ No newline at end of file
{ {
"minSdkVersion": "19" "minSdkVersion": "19"
} }
\ No newline at end of file
export const ACTION_DOWNLOAD_FINISH = "ACTION_DOWNLOAD_FINISH" export const ACTION_DOWNLOAD_FINISH = "ACTION_DOWNLOAD_FINISH"
export const ACTION_DOWNLOAD_PROGRESS = "ACTION_DOWNLOAD_PROGRESS" export const ACTION_DOWNLOAD_PROGRESS = "ACTION_DOWNLOAD_PROGRESS"
\ No newline at end of file
import Build from 'android.os.Build'; import Build from 'android.os.Build';
import Context from 'android.content.Context'; import Context from 'android.content.Context';
import NotificationManager from 'android.app.NotificationManager'; import NotificationManager from 'android.app.NotificationManager';
import NotificationChannel from 'android.app.NotificationChannel'; import NotificationChannel from 'android.app.NotificationChannel';
import Notification from 'android.app.Notification'; import Notification from 'android.app.Notification';
import Intent from 'android.content.Intent'; import Intent from 'android.content.Intent';
import ComponentName from 'android.content.ComponentName'; import ComponentName from 'android.content.ComponentName';
import PendingIntent from 'android.app.PendingIntent'; import PendingIntent from 'android.app.PendingIntent';
import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts'; import { CreateNotificationProgressOptions, FinishNotificationProgressOptions } from '../interface.uts';
import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts" import { ACTION_DOWNLOAD_FINISH, ACTION_DOWNLOAD_PROGRESS } from "./constant.uts"
export { TransparentActivity } from './TransparentActivity.uts'; export { TransparentActivity } from './TransparentActivity.uts';
const DOWNLOAD_PROGRESS_NOTIFICATION_ID : Int = 7890 const DOWNLOAD_PROGRESS_NOTIFICATION_ID : Int = 7890
const DC_DOWNLOAD_CHANNEL_ID = "下载文件" const DC_DOWNLOAD_CHANNEL_ID = "下载文件"
const DC_DOWNLOAD_CHANNEL_NAME = "用于显示现在进度的渠道" const DC_DOWNLOAD_CHANNEL_NAME = "用于显示现在进度的渠道"
let notificationBuilder : Notification.Builder | null = null let notificationBuilder : Notification.Builder | null = null
let timeId = -1 let timeId = -1
let histroyProgress = 0 let histroyProgress = 0
let isProgress = false let isProgress = false
export let globalNotificationProgressCallBack : (() => void) | null = () => { } export let globalNotificationProgressCallBack : (() => void) | null = () => { }
export let globalNotificationProgressFinishCallBack = () => { } export let globalNotificationProgressFinishCallBack = () => { }
export function createNotificationProgress(options : CreateNotificationProgressOptions) : void { export function createNotificationProgress(options : CreateNotificationProgressOptions) : void {
const { content, progress, onClick } = options const { content, progress, onClick } = options
if (progress == 100) { if (progress == 100) {
clearTimeout(timeId) clearTimeout(timeId)
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick) realCreateNotificationProgress(options.title ?? getAppName(context), content, progress, onClick)
reset() reset()
return return
} }
histroyProgress = progress histroyProgress = progress
if (timeId != -1) { if (timeId != -1) {
return return
} }
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
if (!isProgress) { if (!isProgress) {
realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
isProgress = true isProgress = true
} else { } else {
timeId = setTimeout(() => { timeId = setTimeout(() => {
realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick) realCreateNotificationProgress(options.title ?? getAppName(context), content, histroyProgress, onClick)
timeId = -1 timeId = -1
}, 1000) }, 1000)
} }
} }
export function cancelNotificationProgress() : void { export function cancelNotificationProgress() : void {
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID) notificationManager.cancel(DOWNLOAD_PROGRESS_NOTIFICATION_ID)
reset() reset()
} }
function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void { function realCreateNotificationProgress(title : string, content : string, progress : number, cb : (() => void) | null) : void {
globalNotificationProgressCallBack = cb globalNotificationProgressCallBack = cb
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createDownloadChannel(notificationManager) createDownloadChannel(notificationManager)
const builder = createNotificationBuilder(context) const builder = createNotificationBuilder(context)
builder.setProgress(100, progress.toInt(), false) builder.setProgress(100, progress.toInt(), false)
builder.setContentTitle(title) builder.setContentTitle(title)
builder.setContentText(content) builder.setContentText(content)
builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS)); builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_PROGRESS));
notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build())
} }
export function finishNotificationProgress(options : FinishNotificationProgressOptions) { export function finishNotificationProgress(options : FinishNotificationProgressOptions) {
globalNotificationProgressFinishCallBack = options.onClick globalNotificationProgressFinishCallBack = options.onClick
const context = UTSAndroid.getAppContext() as Context const context = UTSAndroid.getAppContext() as Context
const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createDownloadChannel(notificationManager) createDownloadChannel(notificationManager)
const builder = createNotificationBuilder(context) const builder = createNotificationBuilder(context)
builder.setProgress(0, 0, false) builder.setProgress(0, 0, false)
builder.setContentTitle(options.title ?? getAppName(context)) builder.setContentTitle(options.title ?? getAppName(context))
builder.setContentText(options.content) builder.setContentText(options.content)
//小米rom setOngoing未false的时候,会被通知管理器归为不重要通知 //小米rom setOngoing未false的时候,会被通知管理器归为不重要通知
// builder.setOngoing(false) // builder.setOngoing(false)
builder.setAutoCancel(true); builder.setAutoCancel(true);
builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH)); builder.setContentIntent(createPendingIntent(context, ACTION_DOWNLOAD_FINISH));
notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build()) notificationManager.notify(DOWNLOAD_PROGRESS_NOTIFICATION_ID, builder.build())
reset() reset()
} }
function reset() { function reset() {
isProgress = false isProgress = false
notificationBuilder = null notificationBuilder = null
histroyProgress = 0 histroyProgress = 0
if (timeId != -1) { if (timeId != -1) {
clearTimeout(timeId) clearTimeout(timeId)
timeId = -1 timeId = -1
} }
} }
function createPendingIntent(context : Context, action : string) : PendingIntent { function createPendingIntent(context : Context, action : string) : PendingIntent {
const i = new Intent(action); const i = new Intent(action);
i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity")); i.setComponent(new ComponentName(context.getPackageName(), "uts.sdk.modules.utsProgressNotification.TransparentActivity"));
let flags = PendingIntent.FLAG_ONE_SHOT; let flags = PendingIntent.FLAG_ONE_SHOT;
if (Build.VERSION.SDK_INT >= 23) { if (Build.VERSION.SDK_INT >= 23) {
flags = PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE; flags = PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE;
} }
return PendingIntent.getActivity(context, DOWNLOAD_PROGRESS_NOTIFICATION_ID, i, flags); return PendingIntent.getActivity(context, DOWNLOAD_PROGRESS_NOTIFICATION_ID, i, flags);
} }
function createDownloadChannel(notificationManager : NotificationManager) { function createDownloadChannel(notificationManager : NotificationManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
const channel = new NotificationChannel( const channel = new NotificationChannel(
DC_DOWNLOAD_CHANNEL_ID, DC_DOWNLOAD_CHANNEL_ID,
DC_DOWNLOAD_CHANNEL_NAME, DC_DOWNLOAD_CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW NotificationManager.IMPORTANCE_LOW
) )
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(channel)
} }
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
function createNotificationBuilder(context : Context) : Notification.Builder { function createNotificationBuilder(context : Context) : Notification.Builder {
if (notificationBuilder == null) { if (notificationBuilder == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new Notification.Builder(context, DC_DOWNLOAD_CHANNEL_ID) notificationBuilder = new Notification.Builder(context, DC_DOWNLOAD_CHANNEL_ID)
} else { } else {
notificationBuilder = new Notification.Builder(context) notificationBuilder = new Notification.Builder(context)
} }
notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon) notificationBuilder!.setSmallIcon(context.getApplicationInfo().icon)
notificationBuilder!.setOngoing(true) notificationBuilder!.setOngoing(true)
notificationBuilder!.setSound(null) notificationBuilder!.setSound(null)
} }
return notificationBuilder! return notificationBuilder!
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
function getAppName(context : Context) : string { function getAppName(context : Context) : string {
let appName = "" let appName = ""
try { try {
const packageManager = context.getPackageManager() const packageManager = context.getPackageManager()
const applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0) const applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0)
appName = packageManager.getApplicationLabel(applicationInfo) as string appName = packageManager.getApplicationLabel(applicationInfo) as string
} catch (e : Exception) { } catch (e : Exception) {
e.printStackTrace() e.printStackTrace()
} }
return appName return appName
} }
\ No newline at end of file
export type CreateNotificationProgressOptions = { export type CreateNotificationProgressOptions = {
/** /**
* 通知标题 * 通知标题
* @defaultValue 应用名称 * @defaultValue 应用名称
*/ */
title ?: string | null title ?: string | null
/** /**
* 通知内容 * 通知内容
*/ */
content : string, content : string,
/** /**
* 进度 * 进度
*/ */
progress : number, progress : number,
/** /**
* 点击通知消息回调 * 点击通知消息回调
* @defaultValue null * @defaultValue null
*/ */
onClick? : (() => void) | null onClick? : (() => void) | null
} }
export type FinishNotificationProgressOptions = { export type FinishNotificationProgressOptions = {
/** /**
* 通知标题 * 通知标题
* @defaultValue 应用名称 * @defaultValue 应用名称
*/ */
title ?: string | null title ?: string | null
/** /**
* 通知内容 * 通知内容
*/ */
content : string, content : string,
/** /**
* 点击通知消息回调 * 点击通知消息回调
*/ */
onClick : () => void onClick : () => void
} }
export type CreateNotificationProgress = (options : CreateNotificationProgressOptions) => void; export type CreateNotificationProgress = (options : CreateNotificationProgressOptions) => void;
export type CancelNotificationProgress = () => void; export type CancelNotificationProgress = () => void;
export type FinishNotificationProgress = (options: FinishNotificationProgressOptions) => void export type FinishNotificationProgress = (options: FinishNotificationProgressOptions) => void
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册