g***@126.com
g***@126.com
  • 发布:2019-09-24 23:49
  • 更新:2022-10-08 17:12
  • 阅读:3211

Android8.0及以上版本系统使用Notification无法显示问题

分类:Native.js

首先介绍一下背景,我要实现app升级时在通知栏显示进度条。依据帖子https://ask.dcloud.net.cn/article/503 在模拟器上实现无误。但放入真机则发现部分手机无法显示通知栏消息。经排查得出Android8.0及以上系统实现消息通知时需要指定通道。
故有如下代码改动:

/**  
   * @constructor 创建通知栏进度条构造函数  
   */  
  function NotificationCustom() {  
    if (plus.os.name != 'Android') {  
      return;  
    }  
    //当前版本号  
    var SystemVersion = plus.os.version;  
    var Context = plus.android.importClass("android.content.Context");  
    var main = plus.android.runtimeMainActivity();  
    var NotificationManager = plus.android.importClass("android.app.NotificationManager");  
    var nm = main.getSystemService(Context.NOTIFICATION_SERVICE)  
    // Notification build 要android api16以上才能使用(4.1.2以上)  
    // plus.io.convertLocalFileSystemURL("image/1.png")  
    var Notification = null;  
    var firstVersionNumber = Number(SystemVersion.split('.')[0]);  
    console.log("firstVersionNumber:"+firstVersionNumber);  
    if (compareVersion('4.1.1', SystemVersion) == true) {  
      Notification = plus.android.importClass("android.app.Notification");  
    } else {  
      Notification = plus.android.importClass("android.support.v4.app.NotificationCompat");  
    }  
    if (Notification) {  
      this.notifyManager = nm;  
      if(firstVersionNumber>=8){//判断当前系统版本在8.0及以上  
          var NotificationChannel = plus.android.importClass('android.app.NotificationChannel');  
          var channel = new NotificationChannel(defaultNotifyId,'软件更新提醒',NotificationManager.IMPORTANCE_HIGH);  
          console.log(this.notifyManager==null)  
          this.notifyManager.createNotificationChannel(channel);  
          this.mNotificationBuild = new Notification.Builder(main,defaultNotifyId);  
      }else{  
          this.mNotificationBuild = new Notification.Builder(main);  
          //设为true代表常驻状态栏  
          this.mNotificationBuild.setOngoing(false);  
          //this.mNotificationBuild.setNumber(defaultNumber)  
      }  
      this.mNotificationBuild.setContentTitle(defaultTitle);  
      this.mNotificationBuild.setContentText(defaultContent);  
      this.mNotificationBuild.setTicker(defaultTicker);  
      //默认的push图标  
      this.mNotificationBuild.setSmallIcon(17301620);  
      //设置默认声音  
      //console.log('默认:'+plus.android.importClass("android.app.Notification").DEFAULT_SOUND);  
      this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);  
    }  
  };

我对nativejs语法不是太熟悉,之前也没有开发过原生应用,只是根据百度得来的接口说明写下了这个补救代码,但是
this.notifyManager.createNotificationChannel(channel);
这句话一直报错:
[ERROR] reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference;at android.app.NotificationManager.createNotificationChannel
已检测this.notifyManager非空,不清楚问题出在了哪里,请大佬指点

2019-09-24 23:49 负责人:无 分享
已邀请:
蚊子腿细也是肉

蚊子腿细也是肉

请问楼主你解决了吗,我也遇到这个问题

  • g***@126.com (作者)

    还没有,暂时放一边了,任务有点紧,找到答案了别忘了分享一下哈兄弟~

    2019-09-27 09:56

  • g***@126.com (作者)

    问题解决了,我上面的代码基本上可用,改动一下就行了

    var channel = new NotificationChannel(defaultNotifyId,'软件更新提醒',NotificationManager.IMPORTANCE_HIGH);

    this.mNotificationBuild = new Notification.Builder(main,defaultNotifyId);

    这两个地方的defaultNotifyId是数字类型,但是这里需要一个字符串类型,反正我改了就可以使用了,你可以试试

    2019-10-11 17:44

  • 蚊子腿细也是肉

    回复 g***@126.com: 好的谢谢

    2019-11-28 09:21

FullStack

FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866

要回复问题请先登录注册