提交 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 { ...@@ -48,7 +48,6 @@ public class AliyunPushMessageReceiver extends MessageReceiver {
for (Map.Entry<String, String> entry : map.entrySet()) { for (Map.Entry<String, String> entry : map.entrySet()) {
AliyunPushLog.e(REC_TAG, "key " + entry.getKey() + " value " + entry.getValue()); AliyunPushLog.e(REC_TAG, "key " + entry.getKey() + " value " + entry.getValue());
} }
return super.showNotificationNow(context, map); return super.showNotificationNow(context, map);
} }
......
...@@ -27,9 +27,11 @@ import android.os.Looper; ...@@ -27,9 +27,11 @@ import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
...@@ -53,6 +55,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -53,6 +55,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel; private MethodChannel channel;
private Context mContext; private Context mContext;
private AppLifecycleObserver lifecycleObserver;
private boolean showNoticeWhenForeground = false;
public static AliyunPushPlugin sInstance; public static AliyunPushPlugin sInstance;
...@@ -61,6 +65,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -61,6 +65,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
} }
public void callFlutterMethod(String method, Map<String, Object> arguments) { public void callFlutterMethod(String method, Map<String, Object> arguments) {
AliyunPushLog.d(TAG, method);
if (TextUtils.isEmpty(method)) { if (TextUtils.isEmpty(method)) {
return; return;
} }
...@@ -74,6 +79,9 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -74,6 +79,9 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aliyun_push"); channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aliyun_push");
channel.setMethodCallHandler(this); channel.setMethodCallHandler(this);
mContext = flutterPluginBinding.getApplicationContext(); mContext = flutterPluginBinding.getApplicationContext();
// 初始化应用生命周期观察者
lifecycleObserver = new AppLifecycleObserver((Application) mContext);
((Application) mContext).registerActivityLifecycleCallbacks(lifecycleObserver);
} }
@Override @Override
...@@ -105,6 +113,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -105,6 +113,8 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
unbindTag(call, result); unbindTag(call, result);
} else if ("listTags".equals(methodName)) { } else if ("listTags".equals(methodName)) {
listTags(call, result); listTags(call, result);
} else if ("showNoticeWhenForeground".equals(methodName)) {
showNoticeWhenForeground(call, result);
} else if ("bindPhoneNumber".equals(methodName)) { } else if ("bindPhoneNumber".equals(methodName)) {
bindPhoneNumber(call, result); bindPhoneNumber(call, result);
} else if ("unbindPhoneNumber".equals(methodName)) { } else if ("unbindPhoneNumber".equals(methodName)) {
...@@ -141,6 +151,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -141,6 +151,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
@Override @Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null); channel.setMethodCallHandler(null);
((Application) mContext).unregisterActivityLifecycleCallbacks(lifecycleObserver);
} }
private void initPush(MethodCall call, Result result) { private void initPush(MethodCall call, Result result) {
...@@ -189,7 +200,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -189,7 +200,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
Context context = mContext.getApplicationContext(); Context context = mContext.getApplicationContext();
if (context instanceof Application) { if (context instanceof Application) {
Application application = (Application)context; Application application = (Application) context;
AliyunThirdPushUtils.registerHuaweiPush(application); AliyunThirdPushUtils.registerHuaweiPush(application);
AliyunThirdPushUtils.registerXiaoMiPush(application); AliyunThirdPushUtils.registerXiaoMiPush(application);
AliyunThirdPushUtils.registerVivoPush(application); AliyunThirdPushUtils.registerVivoPush(application);
...@@ -319,7 +330,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -319,7 +330,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map.put(ERROR_MSG_KEY, errorMsg); map.put(ERROR_MSG_KEY, errorMsg);
try { try {
result.success(map); result.success(map);
} catch (Exception e){ } catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} }
...@@ -365,7 +376,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -365,7 +376,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map.put(ERROR_MSG_KEY, "alias can not be empty"); map.put(ERROR_MSG_KEY, "alias can not be empty");
try { try {
result.success(map); result.success(map);
} catch (Exception e){ } catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} }
...@@ -406,7 +417,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -406,7 +417,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map.put(ERROR_MSG_KEY, "alias can not be empty"); map.put(ERROR_MSG_KEY, "alias can not be empty");
try { try {
result.success(map); result.success(map);
} catch (Exception e){ } catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} }
...@@ -418,7 +429,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -418,7 +429,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
map.put(CODE_KEY, CODE_SUCCESS); map.put(CODE_KEY, CODE_SUCCESS);
try { try {
result.success(map); result.success(map);
} catch (Exception e){ } catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} }
} }
...@@ -717,7 +728,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -717,7 +728,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
List<Long> vibrationPattern = call.argument("vibrationPattern"); List<Long> vibrationPattern = call.argument("vibrationPattern");
NotificationManager notificationManager NotificationManager notificationManager
= (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
int importanceValue; int importanceValue;
if (importance == null) { if (importance == null) {
importanceValue = NotificationManager.IMPORTANCE_DEFAULT; importanceValue = NotificationManager.IMPORTANCE_DEFAULT;
...@@ -799,7 +810,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -799,7 +810,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
String desc = call.argument("desc"); String desc = call.argument("desc");
NotificationManager notificationManager NotificationManager notificationManager
= (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannelGroup group = new NotificationChannelGroup(id, name); NotificationChannelGroup group = new NotificationChannelGroup(id, name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
group.setDescription(desc); group.setDescription(desc);
...@@ -827,7 +838,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -827,7 +838,7 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
private void isNotificationEnabled(MethodCall call, Result result) { private void isNotificationEnabled(MethodCall call, Result result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager)mContext.getSystemService( NotificationManager manager = (NotificationManager) mContext.getSystemService(
Context.NOTIFICATION_SERVICE); Context.NOTIFICATION_SERVICE);
if (!manager.areNotificationsEnabled()) { if (!manager.areNotificationsEnabled()) {
result.success(false); result.success(false);
...@@ -885,4 +896,19 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -885,4 +896,19 @@ public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
mContext.startActivity(intent); 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();
}
} }
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> { ...@@ -89,6 +89,32 @@ class _AndroidPageState extends BaseState<AndroidPage> {
}, },
child: const Text('清除所有通知')), 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( children.add(
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0), padding: const EdgeInsets.symmetric(horizontal: 10.0),
......
...@@ -23,7 +23,7 @@ class _IOSPageState extends BaseState<IOSPage> { ...@@ -23,7 +23,7 @@ class _IOSPageState extends BaseState<IOSPage> {
padding: const EdgeInsets.symmetric(horizontal: 10.0), padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: ElevatedButton( child: ElevatedButton(
onPressed: () { onPressed: () {
_aliyunPush.showIOSNoticeWhenForeground(true).then((result) { _aliyunPush.showNoticeWhenForeground(true).then((result) {
var code = result['code']; var code = result['code'];
if (code == kAliyunPushSuccessCode) { if (code == kAliyunPushSuccessCode) {
showOkDialog('设置前台显示通知成功'); showOkDialog('设置前台显示通知成功');
...@@ -36,7 +36,7 @@ class _IOSPageState extends BaseState<IOSPage> { ...@@ -36,7 +36,7 @@ class _IOSPageState extends BaseState<IOSPage> {
padding: const EdgeInsets.symmetric(horizontal: 10.0), padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: ElevatedButton( child: ElevatedButton(
onPressed: () { onPressed: () {
_aliyunPush.showIOSNoticeWhenForeground(false).then((result) { _aliyunPush.showNoticeWhenForeground(false).then((result) {
var code = result['code']; var code = result['code'];
if (code == kAliyunPushSuccessCode) { if (code == kAliyunPushSuccessCode) {
showOkDialog('设置前台不显示通知成功'); showOkDialog('设置前台不显示通知成功');
......
...@@ -245,6 +245,16 @@ class AliyunPush { ...@@ -245,6 +245,16 @@ class AliyunPush {
return listResult; 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专用接口 ***************** // ***************** Android专用接口 *****************
///注册厂商通道 ///注册厂商通道
Future<Map<dynamic, dynamic>> initAndroidThirdPush() async { Future<Map<dynamic, dynamic>> initAndroidThirdPush() async {
...@@ -417,19 +427,6 @@ class AliyunPush { ...@@ -417,19 +427,6 @@ class AliyunPush {
return apnsDeviceToken; 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 { Future<bool> isIOSChannelOpened() async {
if (!Platform.isIOS) { if (!Platform.isIOS) {
return false; return false;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论