提交 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;
...@@ -41,848 +43,872 @@ import io.flutter.plugin.common.MethodChannel.Result; ...@@ -41,848 +43,872 @@ import io.flutter.plugin.common.MethodChannel.Result;
*/ */
public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler { public class AliyunPushPlugin implements FlutterPlugin, MethodCallHandler {
private static final String TAG = "MPS:PushPlugin"; private static final String TAG = "MPS:PushPlugin";
private static final String CODE_SUCCESS = "10000"; private static final String CODE_SUCCESS = "10000";
private static final String CODE_PARAM_ILLEGAL = "10001"; private static final String CODE_PARAM_ILLEGAL = "10001";
private static final String CODE_FAILED = "10002"; private static final String CODE_FAILED = "10002";
private static final String CODE_NOT_SUPPORT = "10005"; private static final String CODE_NOT_SUPPORT = "10005";
private static final String CODE_KEY = "code"; private static final String CODE_KEY = "code";
private static final String ERROR_MSG_KEY = "errorMsg"; private static final String ERROR_MSG_KEY = "errorMsg";
private MethodChannel channel; private MethodChannel channel;
private Context mContext; private Context mContext;
private AppLifecycleObserver lifecycleObserver;
public static AliyunPushPlugin sInstance; private boolean showNoticeWhenForeground = false;
public AliyunPushPlugin() { public static AliyunPushPlugin sInstance;
sInstance = this;
} public AliyunPushPlugin() {
sInstance = this;
public void callFlutterMethod(String method, Map<String, Object> arguments) { }
if (TextUtils.isEmpty(method)) {
return; public void callFlutterMethod(String method, Map<String, Object> arguments) {
} AliyunPushLog.d(TAG, method);
if (TextUtils.isEmpty(method)) {
Handler handler = new Handler(Looper.getMainLooper()); return;
handler.post(() -> channel.invokeMethod(method, arguments)); }
}
Handler handler = new Handler(Looper.getMainLooper());
@Override handler.post(() -> channel.invokeMethod(method, arguments));
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { }
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aliyun_push");
channel.setMethodCallHandler(this); @Override
mContext = flutterPluginBinding.getApplicationContext(); public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
} channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "aliyun_push");
channel.setMethodCallHandler(this);
@Override mContext = flutterPluginBinding.getApplicationContext();
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { // 初始化应用生命周期观察者
String methodName = call.method; lifecycleObserver = new AppLifecycleObserver((Application) mContext);
if ("initPush".equals(methodName)) { ((Application) mContext).registerActivityLifecycleCallbacks(lifecycleObserver);
initPush(call, result); }
} else if ("initThirdPush".equals(methodName)) {
initThirdPush(result); @Override
} else if ("getDeviceId".equals(methodName)) { public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
getDeviceId(result); String methodName = call.method;
} else if ("closePushLog".equals(methodName)) { if ("initPush".equals(methodName)) {
closePushLog(result); initPush(call, result);
} else if ("setLogLevel".equals(methodName)) { } else if ("initThirdPush".equals(methodName)) {
setLogLevel(call, result); initThirdPush(result);
} else if ("bindAccount".equals(methodName)) { } else if ("getDeviceId".equals(methodName)) {
bindAccount(call, result); getDeviceId(result);
} else if ("unbindAccount".equals(methodName)) { } else if ("closePushLog".equals(methodName)) {
unbindAccount(result); closePushLog(result);
} else if ("addAlias".equals(methodName)) { } else if ("setLogLevel".equals(methodName)) {
addAlias(call, result); setLogLevel(call, result);
} else if ("removeAlias".equals(methodName)) { } else if ("bindAccount".equals(methodName)) {
removeAlias(call, result); bindAccount(call, result);
} else if ("listAlias".equals(methodName)) { } else if ("unbindAccount".equals(methodName)) {
listAlias(result); unbindAccount(result);
} else if ("bindTag".equals(methodName)) { } else if ("addAlias".equals(methodName)) {
bindTag(call, result); addAlias(call, result);
} else if ("unbindTag".equals(methodName)) { } else if ("removeAlias".equals(methodName)) {
unbindTag(call, result); removeAlias(call, result);
} else if ("listTags".equals(methodName)) { } else if ("listAlias".equals(methodName)) {
listTags(call, result); listAlias(result);
} else if ("bindPhoneNumber".equals(methodName)) { } else if ("bindTag".equals(methodName)) {
bindPhoneNumber(call, result); bindTag(call, result);
} else if ("unbindPhoneNumber".equals(methodName)) { } else if ("unbindTag".equals(methodName)) {
unbindPhoneNumber(result); unbindTag(call, result);
} else if ("setNotificationInGroup".equals(methodName)) { } else if ("listTags".equals(methodName)) {
setNotificationInGroup(call, result); listTags(call, result);
} else if ("clearNotifications".equals(methodName)) { } else if ("showNoticeWhenForeground".equals(methodName)) {
clearNotifications(result); showNoticeWhenForeground(call, result);
} else if ("createChannel".equals(methodName)) { } else if ("bindPhoneNumber".equals(methodName)) {
createChannel(call, result); bindPhoneNumber(call, result);
} else if ("createGroup".equals(methodName)) { } else if ("unbindPhoneNumber".equals(methodName)) {
createGroup(call, result); unbindPhoneNumber(result);
} else if ("isNotificationEnabled".equals(methodName)) { } else if ("setNotificationInGroup".equals(methodName)) {
setNotificationInGroup(call, result);
try { } else if ("clearNotifications".equals(methodName)) {
isNotificationEnabled(call, result); clearNotifications(result);
} catch (Exception e) { } else if ("createChannel".equals(methodName)) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); createChannel(call, result);
} } else if ("createGroup".equals(methodName)) {
} else if ("jumpToNotificationSettings".equals(methodName)) { createGroup(call, result);
if (VERSION.SDK_INT >= VERSION_CODES.O) { } else if ("isNotificationEnabled".equals(methodName)) {
jumpToAndroidNotificationSettings(call);
} try {
} else if ("setPluginLogEnabled".equals(methodName)) { isNotificationEnabled(call, result);
Boolean enabled = call.argument("enabled"); } catch (Exception e) {
if (enabled != null) { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
AliyunPushLog.setLogEnabled(enabled); }
} } else if ("jumpToNotificationSettings".equals(methodName)) {
} else { if (VERSION.SDK_INT >= VERSION_CODES.O) {
result.notImplemented(); jumpToAndroidNotificationSettings(call);
} }
} } else if ("setPluginLogEnabled".equals(methodName)) {
Boolean enabled = call.argument("enabled");
@Override if (enabled != null) {
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { AliyunPushLog.setLogEnabled(enabled);
channel.setMethodCallHandler(null); }
} } else {
result.notImplemented();
private void initPush(MethodCall call, Result result) { }
String appKey = call.argument("appKey"); }
String appSecret = call.argument("appSecret");
if (TextUtils.isEmpty(appKey) || TextUtils.isEmpty(appSecret) || !(mContext instanceof Application)) { @Override
PushServiceFactory.init(mContext); public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
} else { channel.setMethodCallHandler(null);
PushInitConfig config = new PushInitConfig.Builder() ((Application) mContext).unregisterActivityLifecycleCallbacks(lifecycleObserver);
.application((Application) mContext) }
.appKey(appKey)
.appSecret(appSecret) private void initPush(MethodCall call, Result result) {
.build(); String appKey = call.argument("appKey");
PushServiceFactory.init(config); String appSecret = call.argument("appSecret");
} if (TextUtils.isEmpty(appKey) || TextUtils.isEmpty(appSecret) || !(mContext instanceof Application)) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); PushServiceFactory.init(mContext);
pushService.setLogLevel(CloudPushService.LOG_DEBUG); } else {
pushService.register(mContext, new CommonCallback() { PushInitConfig config = new PushInitConfig.Builder()
@Override .application((Application) mContext)
public void onSuccess(String response) { .appKey(appKey)
HashMap<String, String> map = new HashMap<>(); .appSecret(appSecret)
map.put(CODE_KEY, CODE_SUCCESS); .build();
try { PushServiceFactory.init(config);
result.success(map); }
} catch (Exception e) { final CloudPushService pushService = PushServiceFactory.getCloudPushService();
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); pushService.setLogLevel(CloudPushService.LOG_DEBUG);
} pushService.register(mContext, new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override HashMap<String, String> map = new HashMap<>();
public void onFailed(String errorCode, String errorMessage) { map.put(CODE_KEY, CODE_SUCCESS);
HashMap<String, String> map = new HashMap<>(); try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMessage); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} @Override
} public void onFailed(String errorCode, String errorMessage) {
}); HashMap<String, String> map = new HashMap<>();
} map.put(CODE_KEY, errorCode);
map.put(ERROR_MSG_KEY, errorMessage);
private void initThirdPush(Result result) { try {
result.success(map);
HashMap<String, String> map = new HashMap<>(); } catch (Exception e) {
Context context = mContext.getApplicationContext(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (context instanceof Application) { }
Application application = (Application)context; }
AliyunThirdPushUtils.registerHuaweiPush(application); });
AliyunThirdPushUtils.registerXiaoMiPush(application); }
AliyunThirdPushUtils.registerVivoPush(application);
AliyunThirdPushUtils.registerOppoPush(application); private void initThirdPush(Result result) {
AliyunThirdPushUtils.registerMeizuPush(application);
AliyunThirdPushUtils.registerGCM(application); HashMap<String, String> map = new HashMap<>();
AliyunThirdPushUtils.registerHonorPush(application); Context context = mContext.getApplicationContext();
if (context instanceof Application) {
map.put(CODE_KEY, CODE_SUCCESS); Application application = (Application) context;
} else { AliyunThirdPushUtils.registerHuaweiPush(application);
map.put(CODE_KEY, CODE_FAILED); AliyunThirdPushUtils.registerXiaoMiPush(application);
map.put(ERROR_MSG_KEY, "context is not Application"); AliyunThirdPushUtils.registerVivoPush(application);
} AliyunThirdPushUtils.registerOppoPush(application);
AliyunThirdPushUtils.registerMeizuPush(application);
try { AliyunThirdPushUtils.registerGCM(application);
result.success(map); AliyunThirdPushUtils.registerHonorPush(application);
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); map.put(CODE_KEY, CODE_SUCCESS);
} } else {
map.put(CODE_KEY, CODE_FAILED);
} map.put(ERROR_MSG_KEY, "context is not Application");
}
private void closePushLog(Result result) {
CloudPushService service = PushServiceFactory.getCloudPushService(); try {
service.setLogLevel(CloudPushService.LOG_OFF); result.success(map);
HashMap<String, String> map = new HashMap<>(); } catch (Exception e) {
map.put(CODE_KEY, CODE_SUCCESS); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
}
try {
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void closePushLog(Result result) {
} CloudPushService service = PushServiceFactory.getCloudPushService();
service.setLogLevel(CloudPushService.LOG_OFF);
} HashMap<String, String> map = new HashMap<>();
map.put(CODE_KEY, CODE_SUCCESS);
private void getDeviceId(Result result) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); try {
String deviceId = pushService.getDeviceId(); result.success(map);
try { } catch (Exception e) {
result.success(deviceId); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} }
} private void getDeviceId(Result result) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService();
private void setLogLevel(MethodCall call, Result result) { String deviceId = pushService.getDeviceId();
try { try {
String level = call.argument("level"); result.success(deviceId);
if (level == null || level.isEmpty()) { } catch (Exception e) {
handleError(result, CODE_PARAM_ILLEGAL, "Log level is empty"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
return; }
}
}
Integer logLevel = getLogLevel(level.toLowerCase());
if (logLevel == null) { private void setLogLevel(MethodCall call, Result result) {
handleError(result, CODE_PARAM_ILLEGAL, "Invalid log level: " + level); try {
return; String level = call.argument("level");
} if (level == null || level.isEmpty()) {
handleError(result, CODE_PARAM_ILLEGAL, "Log level is empty");
PushServiceFactory.getCloudPushService().setLogLevel(logLevel); return;
handleSuccess(result); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); Integer logLevel = getLogLevel(level.toLowerCase());
handleError(result, CODE_FAILED, e.getMessage()); if (logLevel == null) {
} handleError(result, CODE_PARAM_ILLEGAL, "Invalid log level: " + level);
} return;
}
private Integer getLogLevel(String level) {
switch (level) { PushServiceFactory.getCloudPushService().setLogLevel(logLevel);
case "none": handleSuccess(result);
return CloudPushService.LOG_OFF; } catch (Exception e) {
case "error": AliyunPushLog.e(TAG, Log.getStackTraceString(e));
case "warn": // warn 映射为 error handleError(result, CODE_FAILED, e.getMessage());
return CloudPushService.LOG_ERROR; }
case "info": }
return CloudPushService.LOG_INFO;
case "debug": private Integer getLogLevel(String level) {
return CloudPushService.LOG_DEBUG; switch (level) {
default: case "none":
return null; return CloudPushService.LOG_OFF;
} case "error":
} case "warn": // warn 映射为 error
return CloudPushService.LOG_ERROR;
private void handleSuccess(Result result) { case "info":
HashMap<String, String> map = new HashMap<>(); return CloudPushService.LOG_INFO;
map.put(CODE_KEY, CODE_SUCCESS); case "debug":
result.success(map); return CloudPushService.LOG_DEBUG;
} default:
return null;
private void handleError(Result result, String code, String errorMsg) { }
HashMap<String, String> map = new HashMap<>(); }
map.put(CODE_KEY, code);
map.put(ERROR_MSG_KEY, errorMsg); private void handleSuccess(Result result) {
result.success(map); HashMap<String, String> map = new HashMap<>();
} map.put(CODE_KEY, CODE_SUCCESS);
result.success(map);
private void bindAccount(MethodCall call, Result result) { }
String account = call.argument("account");
HashMap<String, String> map = new HashMap<>(); private void handleError(Result result, String code, String errorMsg) {
if (TextUtils.isEmpty(account)) { HashMap<String, String> map = new HashMap<>();
map.put(CODE_KEY, CODE_PARAM_ILLEGAL); map.put(CODE_KEY, code);
map.put(ERROR_MSG_KEY, "account can not be empty"); map.put(ERROR_MSG_KEY, errorMsg);
try { result.success(map);
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void bindAccount(MethodCall call, Result result) {
} String account = call.argument("account");
HashMap<String, String> map = new HashMap<>();
} else { if (TextUtils.isEmpty(account)) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
pushService.bindAccount(account, new CommonCallback() { map.put(ERROR_MSG_KEY, "account can not be empty");
@Override try {
public void onSuccess(String response) { result.success(map);
map.put(CODE_KEY, CODE_SUCCESS); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); } else {
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.bindAccount(account, new CommonCallback() {
@Override
@Override public void onSuccess(String response) {
public void onFailed(String errorCode, String errorMsg) { map.put(CODE_KEY, CODE_SUCCESS);
map.put(CODE_KEY, errorCode); try {
map.put(ERROR_MSG_KEY, errorMsg); result.success(map);
try { } catch (Exception e) {
result.success(map); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e){ }
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
} try {
result.success(map);
private void unbindAccount(Result result) { } catch (Exception e) {
HashMap<String, String> map = new HashMap<>(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); }
pushService.unbindAccount(new CommonCallback() {
@Override }
public void onSuccess(String response) { });
map.put(CODE_KEY, CODE_SUCCESS); }
try { }
result.success(map);
} catch (Exception e) { private void unbindAccount(Result result) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); HashMap<String, String> map = new HashMap<>();
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.unbindAccount(new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override map.put(CODE_KEY, CODE_SUCCESS);
public void onFailed(String errorCode, String errorMsg) { try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMsg); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
try {
private void addAlias(MethodCall call, Result result) { result.success(map);
HashMap<String, String> map = new HashMap<>(); } catch (Exception e) {
String alias = call.argument("alias"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (TextUtils.isEmpty(alias)) { }
map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
map.put(ERROR_MSG_KEY, "alias can not be empty"); }
try { });
result.success(map); }
} catch (Exception e){
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void addAlias(MethodCall call, Result result) {
} HashMap<String, String> map = new HashMap<>();
String alias = call.argument("alias");
} else { if (TextUtils.isEmpty(alias)) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
pushService.addAlias(alias, new CommonCallback() { map.put(ERROR_MSG_KEY, "alias can not be empty");
@Override try {
public void onSuccess(String response) { result.success(map);
map.put(CODE_KEY, CODE_SUCCESS); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); } else {
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.addAlias(alias, new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override map.put(CODE_KEY, CODE_SUCCESS);
public void onFailed(String errorCode, String errorMsg) { try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMsg); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
} try {
result.success(map);
private void removeAlias(MethodCall call, Result result) { } catch (Exception e) {
HashMap<String, String> map = new HashMap<>(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
String alias = call.argument("alias"); }
if (TextUtils.isEmpty(alias)) {
map.put(CODE_KEY, CODE_PARAM_ILLEGAL); }
map.put(ERROR_MSG_KEY, "alias can not be empty"); });
try { }
result.success(map); }
} catch (Exception e){
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void removeAlias(MethodCall call, Result result) {
} HashMap<String, String> map = new HashMap<>();
String alias = call.argument("alias");
} else { if (TextUtils.isEmpty(alias)) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
pushService.removeAlias(alias, new CommonCallback() { map.put(ERROR_MSG_KEY, "alias can not be empty");
@Override try {
public void onSuccess(String response) { result.success(map);
map.put(CODE_KEY, CODE_SUCCESS); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e){
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); } else {
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.removeAlias(alias, new CommonCallback() {
@Override
@Override public void onSuccess(String response) {
public void onFailed(String errorCode, String errorMsg) { map.put(CODE_KEY, CODE_SUCCESS);
map.put(CODE_KEY, errorCode); try {
map.put(ERROR_MSG_KEY, errorMsg); result.success(map);
try { } catch (Exception e) {
result.success(map); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
} try {
result.success(map);
private void listAlias(Result result) { } catch (Exception e) {
HashMap<String, String> map = new HashMap<>(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); }
pushService.listAliases(new CommonCallback() {
@Override }
public void onSuccess(String response) { });
map.put(CODE_KEY, CODE_SUCCESS); }
map.put("aliasList", response); }
try {
result.success(map); private void listAlias(Result result) {
} catch (Exception e) { HashMap<String, String> map = new HashMap<>();
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.listAliases(new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override map.put(CODE_KEY, CODE_SUCCESS);
public void onFailed(String errorCode, String errorMsg) { map.put("aliasList", response);
map.put(CODE_KEY, errorCode); try {
map.put(ERROR_MSG_KEY, errorMsg); result.success(map);
try { } catch (Exception e) {
result.success(map); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
} @Override
}); public void onFailed(String errorCode, String errorMsg) {
} map.put(CODE_KEY, errorCode);
map.put(ERROR_MSG_KEY, errorMsg);
private void bindTag(MethodCall call, Result result) { try {
List<String> tags = call.argument("tags"); result.success(map);
HashMap<String, String> map = new HashMap<>(); } catch (Exception e) {
if (tags == null || tags.isEmpty()) { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
map.put(CODE_KEY, CODE_PARAM_ILLEGAL); }
map.put(ERROR_MSG_KEY, "tags can not be empty"); }
try { });
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void bindTag(MethodCall call, Result result) {
} List<String> tags = call.argument("tags");
HashMap<String, String> map = new HashMap<>();
} else { if (tags == null || tags.isEmpty()) {
Integer target = call.argument("target"); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
if (target == null) { map.put(ERROR_MSG_KEY, "tags can not be empty");
//默认本设备 try {
target = 1; result.success(map);
} } catch (Exception e) {
String alias = call.argument("alias"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
String[] tagsArray = new String[tags.size()]; }
tagsArray = tags.toArray(tagsArray);
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); } else {
pushService.bindTag(target, tagsArray, alias, new CommonCallback() { Integer target = call.argument("target");
@Override if (target == null) {
public void onSuccess(String response) { //默认本设备
map.put(CODE_KEY, CODE_SUCCESS); target = 1;
try { }
result.success(map); String alias = call.argument("alias");
} catch (Exception e) { String[] tagsArray = new String[tags.size()];
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); tagsArray = tags.toArray(tagsArray);
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.bindTag(target, tagsArray, alias, new CommonCallback() {
@Override
@Override public void onSuccess(String response) {
public void onFailed(String errorCode, String errorMsg) { map.put(CODE_KEY, CODE_SUCCESS);
map.put(CODE_KEY, errorCode); try {
map.put(ERROR_MSG_KEY, errorMsg); result.success(map);
try { } catch (Exception e) {
result.success(map); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
} @Override
}); public void onFailed(String errorCode, String errorMsg) {
} map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
try {
private void unbindTag(MethodCall call, Result result) { result.success(map);
List<String> tags = call.argument("tags"); } catch (Exception e) {
HashMap<String, String> map = new HashMap<>(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (tags == null || tags.isEmpty()) { }
map.put(CODE_KEY, CODE_PARAM_ILLEGAL); }
map.put(ERROR_MSG_KEY, "tags can not be empty"); });
try { }
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void unbindTag(MethodCall call, Result result) {
} List<String> tags = call.argument("tags");
HashMap<String, String> map = new HashMap<>();
} else { if (tags == null || tags.isEmpty()) {
Integer target = call.argument("target"); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
if (target == null) { map.put(ERROR_MSG_KEY, "tags can not be empty");
//默认本设备 try {
target = 1; result.success(map);
} } catch (Exception e) {
String alias = call.argument("alias"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
String[] tagsArray = new String[tags.size()]; }
tagsArray = tags.toArray(tagsArray);
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); } else {
pushService.unbindTag(target, tagsArray, alias, new CommonCallback() { Integer target = call.argument("target");
@Override if (target == null) {
public void onSuccess(String response) { //默认本设备
map.put(CODE_KEY, CODE_SUCCESS); target = 1;
try { }
result.success(map); String alias = call.argument("alias");
} catch (Exception e) { String[] tagsArray = new String[tags.size()];
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); tagsArray = tags.toArray(tagsArray);
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.unbindTag(target, tagsArray, alias, new CommonCallback() {
@Override
@Override public void onSuccess(String response) {
public void onFailed(String errorCode, String errorMsg) { map.put(CODE_KEY, CODE_SUCCESS);
map.put(CODE_KEY, errorCode); try {
map.put(ERROR_MSG_KEY, errorMsg); result.success(map);
try { } catch (Exception e) {
result.success(map); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
} try {
result.success(map);
private void listTags(MethodCall call, Result result) { } catch (Exception e) {
Integer target = call.argument("target"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (target == null) { }
//默认本设备
target = 1; }
} });
HashMap<String, String> map = new HashMap<>(); }
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); }
pushService.listTags(target, new CommonCallback() {
@Override private void listTags(MethodCall call, Result result) {
public void onSuccess(String response) { Integer target = call.argument("target");
map.put(CODE_KEY, CODE_SUCCESS); if (target == null) {
map.put("tagsList", response); //默认本设备
try { target = 1;
result.success(map); }
} catch (Exception e) { HashMap<String, String> map = new HashMap<>();
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.listTags(target, new CommonCallback() {
@Override
} public void onSuccess(String response) {
map.put(CODE_KEY, CODE_SUCCESS);
@Override map.put("tagsList", response);
public void onFailed(String errorCode, String errorMsg) { try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMsg); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
try {
private void bindPhoneNumber(MethodCall call, Result result) { result.success(map);
HashMap<String, String> map = new HashMap<>(); } catch (Exception e) {
String phone = call.argument("phone"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (TextUtils.isEmpty(phone)) { }
map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
map.put(ERROR_MSG_KEY, "phone number can not be empty"); }
try { });
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); private void bindPhoneNumber(MethodCall call, Result result) {
} HashMap<String, String> map = new HashMap<>();
String phone = call.argument("phone");
} else { if (TextUtils.isEmpty(phone)) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); map.put(CODE_KEY, CODE_PARAM_ILLEGAL);
pushService.bindPhoneNumber(phone, new CommonCallback() { map.put(ERROR_MSG_KEY, "phone number can not be empty");
@Override try {
public void onSuccess(String response) { result.success(map);
map.put(CODE_KEY, CODE_SUCCESS); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); } else {
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.bindPhoneNumber(phone, new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override map.put(CODE_KEY, CODE_SUCCESS);
public void onFailed(String errorCode, String errorMsg) { try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMsg); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
} try {
result.success(map);
private void unbindPhoneNumber(Result result) { } catch (Exception e) {
HashMap<String, String> map = new HashMap<>(); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
}
final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.unbindPhoneNumber(new CommonCallback() { }
@Override });
public void onSuccess(String response) { }
map.put(CODE_KEY, CODE_SUCCESS); }
try {
result.success(map); private void unbindPhoneNumber(Result result) {
} catch (Exception e) { HashMap<String, String> map = new HashMap<>();
AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.unbindPhoneNumber(new CommonCallback() {
} @Override
public void onSuccess(String response) {
@Override map.put(CODE_KEY, CODE_SUCCESS);
public void onFailed(String errorCode, String errorMsg) { try {
map.put(CODE_KEY, errorCode); result.success(map);
map.put(ERROR_MSG_KEY, errorMsg); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); }
}
@Override
} public void onFailed(String errorCode, String errorMsg) {
}); map.put(CODE_KEY, errorCode);
} map.put(ERROR_MSG_KEY, errorMsg);
try {
private void setNotificationInGroup(MethodCall call, Result result) { result.success(map);
Boolean inGroup = call.argument("inGroup"); } catch (Exception e) {
if (inGroup == null) { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
inGroup = false; }
}
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); }
pushService.setNotificationShowInGroup(inGroup); });
HashMap<String, String> map = new HashMap<>(); }
map.put(CODE_KEY, CODE_SUCCESS);
try { private void setNotificationInGroup(MethodCall call, Result result) {
result.success(map); Boolean inGroup = call.argument("inGroup");
} catch (Exception e) { if (inGroup == null) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); inGroup = false;
} }
} final CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.setNotificationShowInGroup(inGroup);
private void clearNotifications(Result result) { HashMap<String, String> map = new HashMap<>();
final CloudPushService pushService = PushServiceFactory.getCloudPushService(); map.put(CODE_KEY, CODE_SUCCESS);
pushService.clearNotifications(); try {
HashMap<String, String> map = new HashMap<>(); result.success(map);
map.put(CODE_KEY, CODE_SUCCESS); } catch (Exception e) {
try { AliyunPushLog.e(TAG, Log.getStackTraceString(e));
result.success(map); }
} catch (Exception e) { }
AliyunPushLog.e(TAG, Log.getStackTraceString(e));
} private void clearNotifications(Result result) {
final CloudPushService pushService = PushServiceFactory.getCloudPushService();
} pushService.clearNotifications();
HashMap<String, String> map = new HashMap<>();
private void createChannel(MethodCall call, Result result) { map.put(CODE_KEY, CODE_SUCCESS);
HashMap<String, String> map = new HashMap<>(); try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { result.success(map);
} catch (Exception e) {
String id = call.argument("id"); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
String name = call.argument("name"); }
Integer importance = call.argument("importance");
String desc = call.argument("desc"); }
String groupId = call.argument("groupId");
Boolean allowBubbles = call.argument("allowBubbles"); private void createChannel(MethodCall call, Result result) {
Boolean light = call.argument("light"); HashMap<String, String> map = new HashMap<>();
Integer color = call.argument("lightColor"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Boolean showBadge = call.argument("showBadge");
String soundPath = call.argument("soundPath"); String id = call.argument("id");
Integer soundUsage = call.argument("soundUsage"); String name = call.argument("name");
Integer soundContentType = call.argument("soundContentType"); Integer importance = call.argument("importance");
Integer soundFlag = call.argument("soundFlag"); String desc = call.argument("desc");
Boolean vibration = call.argument("vibration"); String groupId = call.argument("groupId");
List<Long> vibrationPattern = call.argument("vibrationPattern"); Boolean allowBubbles = call.argument("allowBubbles");
Boolean light = call.argument("light");
NotificationManager notificationManager Integer color = call.argument("lightColor");
= (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); Boolean showBadge = call.argument("showBadge");
int importanceValue; String soundPath = call.argument("soundPath");
if (importance == null) { Integer soundUsage = call.argument("soundUsage");
importanceValue = NotificationManager.IMPORTANCE_DEFAULT; Integer soundContentType = call.argument("soundContentType");
} else { Integer soundFlag = call.argument("soundFlag");
importanceValue = importance; Boolean vibration = call.argument("vibration");
} List<Long> vibrationPattern = call.argument("vibrationPattern");
NotificationChannel channel = new NotificationChannel(id, name, importanceValue);
channel.setDescription(desc); NotificationManager notificationManager
if (groupId != null) { = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
channel.setGroup(groupId); int importanceValue;
} if (importance == null) {
if (allowBubbles != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { importanceValue = NotificationManager.IMPORTANCE_DEFAULT;
channel.setAllowBubbles(allowBubbles); } else {
} importanceValue = importance;
if (light != null) { }
channel.enableLights(light); NotificationChannel channel = new NotificationChannel(id, name, importanceValue);
} channel.setDescription(desc);
if (color != null) { if (groupId != null) {
channel.setLightColor(color); channel.setGroup(groupId);
} }
if (showBadge != null) { if (allowBubbles != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
channel.setShowBadge(showBadge); channel.setAllowBubbles(allowBubbles);
} }
if (!TextUtils.isEmpty(soundPath)) { if (light != null) {
File file = new File(soundPath); channel.enableLights(light);
if (file.exists() && file.canRead() && file.isFile()) { }
if (soundUsage == null) { if (color != null) {
channel.setSound(Uri.fromFile(file), null); channel.setLightColor(color);
} else { }
AudioAttributes.Builder builder = new AudioAttributes.Builder() if (showBadge != null) {
.setUsage(soundUsage); channel.setShowBadge(showBadge);
if (soundContentType != null) { }
builder.setContentType(soundContentType); if (!TextUtils.isEmpty(soundPath)) {
} File file = new File(soundPath);
if (soundFlag != null) { if (file.exists() && file.canRead() && file.isFile()) {
builder.setFlags(soundFlag); if (soundUsage == null) {
} channel.setSound(Uri.fromFile(file), null);
channel.setSound(Uri.fromFile(file), builder.build()); } else {
} AudioAttributes.Builder builder = new AudioAttributes.Builder()
} .setUsage(soundUsage);
} if (soundContentType != null) {
if (vibration != null) { builder.setContentType(soundContentType);
channel.enableVibration(vibration); }
} if (soundFlag != null) {
if (vibrationPattern != null && vibrationPattern.size() > 0) { builder.setFlags(soundFlag);
long[] pattern = new long[vibrationPattern.size()]; }
for (int i = 0; i < vibrationPattern.size(); i++) { channel.setSound(Uri.fromFile(file), builder.build());
pattern[i] = vibrationPattern.get(i); }
} }
channel.setVibrationPattern(pattern); }
} if (vibration != null) {
notificationManager.createNotificationChannel(channel); channel.enableVibration(vibration);
map.put(CODE_KEY, CODE_SUCCESS); }
try { if (vibrationPattern != null && vibrationPattern.size() > 0) {
result.success(map); long[] pattern = new long[vibrationPattern.size()];
} catch (Exception e) { for (int i = 0; i < vibrationPattern.size(); i++) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); pattern[i] = vibrationPattern.get(i);
} }
channel.setVibrationPattern(pattern);
} else { }
map.put(CODE_KEY, CODE_NOT_SUPPORT); notificationManager.createNotificationChannel(channel);
map.put(ERROR_MSG_KEY, map.put(CODE_KEY, CODE_SUCCESS);
"Android version is below Android O which is not support create channel"); 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)); }
}
} else {
} map.put(CODE_KEY, CODE_NOT_SUPPORT);
} map.put(ERROR_MSG_KEY,
"Android version is below Android O which is not support create channel");
private void createGroup(MethodCall call, Result result) { try {
HashMap<String, String> map = new HashMap<>(); result.success(map);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { } catch (Exception e) {
AliyunPushLog.e(TAG, Log.getStackTraceString(e));
String id = call.argument("id"); }
String name = call.argument("name");
String desc = call.argument("desc"); }
}
NotificationManager notificationManager
= (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); private void createGroup(MethodCall call, Result result) {
NotificationChannelGroup group = new NotificationChannelGroup(id, name); HashMap<String, String> map = new HashMap<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
group.setDescription(desc);
} String id = call.argument("id");
notificationManager.createNotificationChannelGroup(group); String name = call.argument("name");
map.put(CODE_KEY, CODE_SUCCESS); String desc = call.argument("desc");
try {
result.success(map); NotificationManager notificationManager
} catch (Exception e) { = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
AliyunPushLog.e(TAG, Log.getStackTraceString(e)); NotificationChannelGroup group = new NotificationChannelGroup(id, name);
} if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
group.setDescription(desc);
} else { }
map.put(CODE_KEY, CODE_NOT_SUPPORT); notificationManager.createNotificationChannelGroup(group);
map.put(ERROR_MSG_KEY, map.put(CODE_KEY, CODE_SUCCESS);
"Android version is below Android O which is not support create group"); 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)); }
}
} else {
} map.put(CODE_KEY, CODE_NOT_SUPPORT);
} map.put(ERROR_MSG_KEY,
"Android version is below Android O which is not support create group");
private void isNotificationEnabled(MethodCall call, Result result) { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { result.success(map);
NotificationManager manager = (NotificationManager)mContext.getSystemService( } catch (Exception e) {
Context.NOTIFICATION_SERVICE); AliyunPushLog.e(TAG, Log.getStackTraceString(e));
if (!manager.areNotificationsEnabled()) { }
result.success(false);
return; }
} }
String id = call.argument("id");
if (id == null) { private void isNotificationEnabled(MethodCall call, Result result) {
result.success(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return; NotificationManager manager = (NotificationManager) mContext.getSystemService(
} Context.NOTIFICATION_SERVICE);
List<NotificationChannel> channels = manager.getNotificationChannels(); if (!manager.areNotificationsEnabled()) {
for (NotificationChannel channel : channels) { result.success(false);
if (channel.getId().equals(id)) { return;
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { }
result.success(false); String id = call.argument("id");
} else { if (id == null) {
if (channel.getGroup() != null) { result.success(true);
if (android.os.Build.VERSION.SDK_INT return;
>= android.os.Build.VERSION_CODES.P) { }
NotificationChannelGroup group List<NotificationChannel> channels = manager.getNotificationChannels();
= manager.getNotificationChannelGroup(channel.getGroup()); for (NotificationChannel channel : channels) {
result.success(!group.isBlocked()); if (channel.getId().equals(id)) {
return; if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
} result.success(false);
} } else {
result.success(true); if (channel.getGroup() != null) {
return; if (android.os.Build.VERSION.SDK_INT
} >= android.os.Build.VERSION_CODES.P) {
} NotificationChannelGroup group
} = manager.getNotificationChannelGroup(channel.getGroup());
// channel 未定义,返回false result.success(!group.isBlocked());
result.success(false); return;
} else { }
boolean enabled = NotificationManagerCompat.from(mContext).areNotificationsEnabled(); }
result.success(enabled); result.success(true);
} return;
} }
}
@RequiresApi(api = VERSION_CODES.O) }
private void jumpToAndroidNotificationSettings(MethodCall call) { // channel 未定义,返回false
String id = call.argument("id"); result.success(false);
Intent intent; } else {
if (id != null) { boolean enabled = NotificationManagerCompat.from(mContext).areNotificationsEnabled();
intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); result.success(enabled);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName()); }
intent.putExtra(Settings.EXTRA_CHANNEL_ID, id); }
} else {
// 跳转到应用的通知设置界面 @RequiresApi(api = VERSION_CODES.O)
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS); private void jumpToAndroidNotificationSettings(MethodCall call) {
intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName()); String id = call.argument("id");
} Intent intent;
if (!(mContext instanceof Activity)) { if (id != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
} intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName());
mContext.startActivity(intent); intent.putExtra(Settings.EXTRA_CHANNEL_ID, id);
} } else {
// 跳转到应用的通知设置界面
intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, mContext.getPackageName());
}
if (!(mContext instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论