1***@qq.com
1***@qq.com
  • 发布:2020-08-15 16:48
  • 更新:2022-09-20 15:27
  • 阅读:2111

【报Bug】uni-app原生态插件开发(android版)中必须初始化必须写在application中的,初始化失败,希望能尽快解决该问题。

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 10.15.5

HBuilderX类型: 正式

HBuilderX版本号: 2.8.6

手机系统: Android

手机系统版本号: Android 9.0

手机厂商: 华为

手机机型: 所以手机

页面类型: nvue

打包方式: 云端

项目创建方式: HBuilderX

操作步骤:

可以编写Android原生态插件集成阿里云移动推送,错误代码如下

public class Push_AppProxy implements AppHookProxy {

private static final String TAG = "Push_AppProxy";  
private static String deviceId = null;  

@Override  
public void onCreate(Application application) {  
    //初始化触发  
    initCloudChannel(application);  
}  

/**  
 * 初始化云推送通道  
 */  
private void initCloudChannel(Application application) {  
    // 创建notificaiton channel  
    this.createNotificationChannel(application);  
    PushServiceFactory.init(application.getApplicationContext());  
    final CloudPushService pushService = PushServiceFactory.getCloudPushService();  
    pushService.register(application, new CommonCallback() {  
        @Override  
        public void onSuccess(String response) {  
            Log.i(TAG, "init cloudchannel success");  
            deviceId = pushService.getDeviceId();  
            Log.i("deviceId", "deviceId = "+deviceId);  
        }  

        @Override  
        public void onFailed(String errorCode, String errorMessage) {  
            Log.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);  
        }  
    });  
    //接入华为辅助推送  
    HuaWeiRegister.register(application);  
  
}  

//创建通知提示  
private void createNotificationChannel(Application application) {  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
        NotificationManager mNotificationManager = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);  
        // 通知渠道的id  
        String id = "1";  
        // 用户可以看到的通知渠道的名字.  
        CharSequence name = "notification channel";  
        // 用户可以看到的通知渠道的描述  
        String description = "notification description";  
        int importance = NotificationManager.IMPORTANCE_HIGH;  
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);  
        // 配置通知渠道的属性  
        mChannel.setDescription(description);  
        // 设置通知出现时的闪灯(如果 android 设备支持的话)  
        mChannel.enableLights(true);  
        mChannel.setLightColor(Color.RED);  
        // 设置通知出现时的震动(如果 android 设备支持的话)  
        mChannel.enableVibration(true);  
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});  
        //最后在notificationmanager中创建该通知渠道  
        mNotificationManager.createNotificationChannel(mChannel);  
    }  
}  

预期结果:

结果就是,继承Application,代码写在Application中,则成功,implements AppHookProxy,则初始化失败

实际结果:

public class MainApplication extends Application {
private static final String TAG = "Init";
private static String deviceId = null;

@Override  
public void onCreate() {  
    super.onCreate();  
    initCloudChannel(this);  
}  

/**  
 * 初始化云推送通道  
 * @param applicationContext  
 */  
private void initCloudChannel(final Context applicationContext) {  
    // 创建notificaiton channel  
    this.createNotificationChannel();  
    PushServiceFactory.init(applicationContext);  
    final CloudPushService pushService = PushServiceFactory.getCloudPushService();  
    pushService.register(applicationContext, new CommonCallback() {  
        @Override  
        public void onSuccess(String response) {  
            Log.i(TAG, "init cloudchannel success");  
            deviceId = pushService.getDeviceId();  

            Log.i("deviceId", "deviceId = "+deviceId);  
        }  

        @Override  
        public void onFailed(String errorCode, String errorMessage) {  
            Log.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);  
        }  
    });  
    //接入华为辅助推送  
    HuaWeiRegister.register(this);  
     
}  

//创建通知提示  
private void createNotificationChannel() {  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
        // 通知渠道的id  
        String id = "1";  
        // 用户可以看到的通知渠道的名字.  
        CharSequence name = "notification channel";  
        // 用户可以看到的通知渠道的描述  
        String description = "notification description";  
        int importance = NotificationManager.IMPORTANCE_HIGH;  
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);  
        // 配置通知渠道的属性  
        mChannel.setDescription(description);  
        // 设置通知出现时的闪灯(如果 android 设备支持的话)  
        mChannel.enableLights(true);  
        mChannel.setLightColor(Color.RED);  
        // 设置通知出现时的震动(如果 android 设备支持的话)  
        mChannel.enableVibration(true);  
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});  
        //最后在notificationmanager中创建该通知渠道  
        mNotificationManager.createNotificationChannel(mChannel);  
    }  
}

bug描述:

【报Bug】uni-app原生态插件开发(android版)中必须初始化必须写在application中的,现在写在implements AppHookProxy实现类onCreate中,初始化失败,典型案例有比如集成阿里云的移动推送等必须在application中初始化的第三方sdk,希望能尽快解决该问题。
具体错误图片如图1,implements AppHookProxy。正确图片为图2,extends Application。

2020-08-15 16:48 负责人:无 分享
已邀请:
DCloud_Android_ST

DCloud_Android_ST

Q:插件SDK部分需要在Application初始化。目前在AppHookProxy中onCreate初始化无效

A: 请使用UniAppHookProxy接口 有onSubProcessCreate子进程初始化回调。将SDK需要在Application初始化的代码onCreate和onSubProcessCreate都放一份就可以了。

参考文档:https://nativesupport.dcloud.net.cn/NativePlugin/course/android

来盘芋头丸

来盘芋头丸 - Android原生JS PHP 都会点

哎。。问题解决了吗 胖友

  • y***@163.com

    大佬,他解没解决我不知道,但是我遇到了相同的问题!!!!qq:1399829329


    2024-02-05 13:27

要回复问题请先登录注册