提交 90adfc27 authored 作者: 史晓晨's avatar 史晓晨

feat:调试Android处于前台时控制通知展示

上级 2a36a2f2
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
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
......@@ -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);
}
......
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) {
}
}
......@@ -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),
......
......@@ -23,7 +23,7 @@ class _IOSPageState extends BaseState<IOSPage> {
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: ElevatedButton(
onPressed: () {
_aliyunPush.showIOSNoticeWhenForeground(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.showIOSNoticeWhenForeground(false).then((result) {
_aliyunPush.showNoticeWhenForeground(false).then((result) {
var code = result['code'];
if (code == kAliyunPushSuccessCode) {
showOkDialog('设置前台不显示通知成功');
......
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论