Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
aliyun_push_clx
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
aliyun_push_clx
Commits
90adfc27
提交
90adfc27
authored
9月 25, 2025
作者:
史晓晨
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:调试Android处于前台时控制通知展示
上级
2a36a2f2
显示空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
147 行增加
和
24 行删除
+147
-24
.gitignore
android/.gitignore
+11
-0
gradle-wrapper.properties
android/gradle/wrapper/gradle-wrapper.properties
+7
-0
AliyunPushMessageReceiver.java
...n/java/com/aliyun/ams/push/AliyunPushMessageReceiver.java
+0
-1
AliyunPushPlugin.java
...d/src/main/java/com/aliyun/ams/push/AliyunPushPlugin.java
+34
-8
AppLifecycleObserver.java
...c/main/java/com/aliyun/ams/push/AppLifecycleObserver.java
+57
-0
android.dart
example/lib/android.dart
+26
-0
ios.dart
example/lib/ios.dart
+2
-2
aliyun_push.dart
lib/aliyun_push.dart
+10
-13
没有找到文件。
android/.gitignore
0 → 100644
浏览文件 @
90adfc27
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java
# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
android/gradle/wrapper/gradle-wrapper.properties
0 → 100644
浏览文件 @
90adfc27
distributionBase
=
GRADLE_USER_HOME
distributionPath
=
wrapper/dists
distributionUrl
=
https
\:
//services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout
=
10000
validateDistributionUrl
=
true
zipStoreBase
=
GRADLE_USER_HOME
zipStorePath
=
wrapper/dists
android/src/main/java/com/aliyun/ams/push/AliyunPushMessageReceiver.java
浏览文件 @
90adfc27
...
...
@@ -48,7 +48,6 @@ public class AliyunPushMessageReceiver extends MessageReceiver {
for
(
Map
.
Entry
<
String
,
String
>
entry
:
map
.
entrySet
())
{
AliyunPushLog
.
e
(
REC_TAG
,
"key "
+
entry
.
getKey
()
+
" value "
+
entry
.
getValue
());
}
return
super
.
showNotificationNow
(
context
,
map
);
}
...
...
android/src/main/java/com/aliyun/ams/push/AliyunPushPlugin.java
浏览文件 @
90adfc27
...
...
@@ -27,9 +27,11 @@ import android.os.Looper;
import
android.provider.Settings
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
androidx.annotation.NonNull
;
import
androidx.annotation.RequiresApi
;
import
androidx.core.app.NotificationManagerCompat
;
import
io.flutter.embedding.engine.plugins.FlutterPlugin
;
import
io.flutter.plugin.common.MethodCall
;
import
io.flutter.plugin.common.MethodChannel
;
...
...
@@ -53,6 +55,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
private
MethodChannel
channel
;
private
Context
mContext
;
private
AppLifecycleObserver
lifecycleObserver
;
private
boolean
showNoticeWhenForeground
=
false
;
public
static
AliyunPushPlugin
sInstance
;
...
...
@@ -61,6 +65,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
}
public
void
callFlutterMethod
(
String
method
,
Map
<
String
,
Object
>
arguments
)
{
AliyunPushLog
.
d
(
TAG
,
method
);
if
(
TextUtils
.
isEmpty
(
method
))
{
return
;
}
...
...
@@ -74,6 +79,9 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
channel
=
new
MethodChannel
(
flutterPluginBinding
.
getBinaryMessenger
(),
"aliyun_push"
);
channel
.
setMethodCallHandler
(
this
);
mContext
=
flutterPluginBinding
.
getApplicationContext
();
// 初始化应用生命周期观察者
lifecycleObserver
=
new
AppLifecycleObserver
((
Application
)
mContext
);
((
Application
)
mContext
).
registerActivityLifecycleCallbacks
(
lifecycleObserver
);
}
@Override
...
...
@@ -105,6 +113,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
unbindTag
(
call
,
result
);
}
else
if
(
"listTags"
.
equals
(
methodName
))
{
listTags
(
call
,
result
);
}
else
if
(
"showNoticeWhenForeground"
.
equals
(
methodName
))
{
showNoticeWhenForeground
(
call
,
result
);
}
else
if
(
"bindPhoneNumber"
.
equals
(
methodName
))
{
bindPhoneNumber
(
call
,
result
);
}
else
if
(
"unbindPhoneNumber"
.
equals
(
methodName
))
{
...
...
@@ -141,6 +151,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
@Override
public
void
onDetachedFromEngine
(
@NonNull
FlutterPluginBinding
binding
)
{
channel
.
setMethodCallHandler
(
null
);
((
Application
)
mContext
).
unregisterActivityLifecycleCallbacks
(
lifecycleObserver
);
}
private
void
initPush
(
MethodCall
call
,
Result
result
)
{
...
...
@@ -189,7 +200,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
HashMap
<
String
,
String
>
map
=
new
HashMap
<>();
Context
context
=
mContext
.
getApplicationContext
();
if
(
context
instanceof
Application
)
{
Application
application
=
(
Application
)
context
;
Application
application
=
(
Application
)
context
;
AliyunThirdPushUtils
.
registerHuaweiPush
(
application
);
AliyunThirdPushUtils
.
registerXiaoMiPush
(
application
);
AliyunThirdPushUtils
.
registerVivoPush
(
application
);
...
...
@@ -319,7 +330,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map
.
put
(
ERROR_MSG_KEY
,
errorMsg
);
try
{
result
.
success
(
map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
AliyunPushLog
.
e
(
TAG
,
Log
.
getStackTraceString
(
e
));
}
...
...
@@ -365,7 +376,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map
.
put
(
ERROR_MSG_KEY
,
"alias can not be empty"
);
try
{
result
.
success
(
map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
AliyunPushLog
.
e
(
TAG
,
Log
.
getStackTraceString
(
e
));
}
...
...
@@ -406,7 +417,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map
.
put
(
ERROR_MSG_KEY
,
"alias can not be empty"
);
try
{
result
.
success
(
map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
AliyunPushLog
.
e
(
TAG
,
Log
.
getStackTraceString
(
e
));
}
...
...
@@ -418,7 +429,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map
.
put
(
CODE_KEY
,
CODE_SUCCESS
);
try
{
result
.
success
(
map
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
AliyunPushLog
.
e
(
TAG
,
Log
.
getStackTraceString
(
e
));
}
}
...
...
@@ -717,7 +728,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
List
<
Long
>
vibrationPattern
=
call
.
argument
(
"vibrationPattern"
);
NotificationManager
notificationManager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
int
importanceValue
;
if
(
importance
==
null
)
{
importanceValue
=
NotificationManager
.
IMPORTANCE_DEFAULT
;
...
...
@@ -799,7 +810,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
String
desc
=
call
.
argument
(
"desc"
);
NotificationManager
notificationManager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
NotificationChannelGroup
group
=
new
NotificationChannelGroup
(
id
,
name
);
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
P
)
{
group
.
setDescription
(
desc
);
...
...
@@ -827,7 +838,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
private
void
isNotificationEnabled
(
MethodCall
call
,
Result
result
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
NotificationManager
manager
=
(
NotificationManager
)
mContext
.
getSystemService
(
NotificationManager
manager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
if
(!
manager
.
areNotificationsEnabled
())
{
result
.
success
(
false
);
...
...
@@ -885,4 +896,19 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
mContext
.
startActivity
(
intent
);
}
private
void
showNoticeWhenForeground
(
MethodCall
call
,
Result
result
)
{
showNoticeWhenForeground
=
call
.
argument
(
"enable"
);
HashMap
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
CODE_KEY
,
CODE_SUCCESS
);
try
{
result
.
success
(
map
);
}
catch
(
Exception
e
)
{
AliyunPushLog
.
e
(
TAG
,
Log
.
getStackTraceString
(
e
));
}
}
public
boolean
showNotice
()
{
return
!
showNoticeWhenForeground
||
lifecycleObserver
.
isAppForeground
();
}
}
android/src/main/java/com/aliyun/ams/push/AppLifecycleObserver.java
0 → 100644
浏览文件 @
90adfc27
package
com
.
aliyun
.
ams
.
push
;
import
android.app.Activity
;
import
android.app.Application
;
import
android.os.Bundle
;
public
class
AppLifecycleObserver
implements
Application
.
ActivityLifecycleCallbacks
{
private
int
activityCount
=
0
;
private
boolean
isAppForeground
=
false
;
public
AppLifecycleObserver
(
Application
application
)
{
application
.
registerActivityLifecycleCallbacks
(
this
);
}
public
boolean
isAppForeground
()
{
return
isAppForeground
;
}
@Override
public
void
onActivityCreated
(
Activity
activity
,
Bundle
savedInstanceState
)
{
}
@Override
public
void
onActivityStarted
(
Activity
activity
)
{
activityCount
++;
// 从后台切换到前台
if
(
activityCount
==
1
)
{
isAppForeground
=
true
;
}
}
@Override
public
void
onActivityResumed
(
Activity
activity
)
{
}
@Override
public
void
onActivityPaused
(
Activity
activity
)
{
}
@Override
public
void
onActivityStopped
(
Activity
activity
)
{
activityCount
--;
// 从前台切换到后台
if
(
activityCount
==
0
)
{
isAppForeground
=
false
;
}
}
@Override
public
void
onActivitySaveInstanceState
(
Activity
activity
,
Bundle
outState
)
{
}
@Override
public
void
onActivityDestroyed
(
Activity
activity
)
{
}
}
example/lib/android.dart
浏览文件 @
90adfc27
...
...
@@ -89,6 +89,32 @@ class _AndroidPageState extends BaseState<AndroidPage> {
},
child:
const
Text
(
'清除所有通知'
)),
));
children
.
add
(
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
10.0
),
child:
ElevatedButton
(
onPressed:
()
{
_aliyunPush
.
showNoticeWhenForeground
(
true
).
then
((
result
)
{
var
code
=
result
[
'code'
];
if
(
code
==
kAliyunPushSuccessCode
)
{
showOkDialog
(
'设置前台显示通知成功'
);
}
});
},
child:
const
Text
(
'前台显示通知'
)),
));
children
.
add
(
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
10.0
),
child:
ElevatedButton
(
onPressed:
()
{
_aliyunPush
.
showNoticeWhenForeground
(
false
).
then
((
result
)
{
var
code
=
result
[
'code'
];
if
(
code
==
kAliyunPushSuccessCode
)
{
showOkDialog
(
'设置前台不显示通知成功'
);
}
});
},
child:
const
Text
(
'前台不显示通知'
)),
));
children
.
add
(
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
10.0
),
...
...
example/lib/ios.dart
浏览文件 @
90adfc27
...
...
@@ -23,7 +23,7 @@ class _IOSPageState extends BaseState<IOSPage> {
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
10.0
),
child:
ElevatedButton
(
onPressed:
()
{
_aliyunPush
.
show
IOS
NoticeWhenForeground
(
true
).
then
((
result
)
{
_aliyunPush
.
showNoticeWhenForeground
(
true
).
then
((
result
)
{
var
code
=
result
[
'code'
];
if
(
code
==
kAliyunPushSuccessCode
)
{
showOkDialog
(
'设置前台显示通知成功'
);
...
...
@@ -36,7 +36,7 @@ class _IOSPageState extends BaseState<IOSPage> {
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
10.0
),
child:
ElevatedButton
(
onPressed:
()
{
_aliyunPush
.
show
IOS
NoticeWhenForeground
(
false
).
then
((
result
)
{
_aliyunPush
.
showNoticeWhenForeground
(
false
).
then
((
result
)
{
var
code
=
result
[
'code'
];
if
(
code
==
kAliyunPushSuccessCode
)
{
showOkDialog
(
'设置前台不显示通知成功'
);
...
...
lib/aliyun_push.dart
浏览文件 @
90adfc27
...
...
@@ -245,6 +245,16 @@ class AliyunPush {
return
listResult
;
}
/// 设置通知在应用前台时是否展示
///
/// @param enable 是否展示
/// @return 返回值
Future
<
Map
<
dynamic
,
dynamic
>>
showNoticeWhenForeground
(
bool
enable
)
async
{
Map
<
dynamic
,
dynamic
>
result
=
await
methodChannel
.
invokeMethod
(
'showNoticeWhenForeground'
,
{
'enable'
:
enable
});
return
result
;
}
// ***************** Android专用接口 *****************
///注册厂商通道
Future
<
Map
<
dynamic
,
dynamic
>>
initAndroidThirdPush
()
async
{
...
...
@@ -417,19 +427,6 @@ class AliyunPush {
return
apnsDeviceToken
;
}
/// 设置iOS通知在应用前台时是否展示
///
/// @param enable 是否展示
/// @return 返回值
Future
<
Map
<
dynamic
,
dynamic
>>
showIOSNoticeWhenForeground
(
bool
enable
)
async
{
if
(!
Platform
.
isIOS
)
{
return
{
'code'
:
kAliyunPushOnlyIOS
,
'errorMsg'
:
'Only support iOS'
};
}
Map
<
dynamic
,
dynamic
>
result
=
await
methodChannel
.
invokeMethod
(
'showNoticeWhenForeground'
,
{
'enable'
:
enable
});
return
result
;
}
Future
<
bool
>
isIOSChannelOpened
()
async
{
if
(!
Platform
.
isIOS
)
{
return
false
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论