cordova 插件中原生调用js

最近搞了个本地推送的cordova插件,里面有这么个需求,在点击推送的本地消息时需要给混合的那边通知下,然后调用某个JS中的方法。

首先我们要知道想要实现原生调用js,那么一定要借助原生中的webview组件。

本文主要是简单说明下cordova插件中的原生是怎样一步步的调用到混合中的js的方法的。

以下以本地通知的插件为例讲解下。

原生中发送本地通知的代码,我这边就不细说了,有不懂的同学直接百度吧。我们直接从点击本地消息的那个动作说起。
这里需要说明一下,我的本地消息推送的PendingIntent 用的是广播,所以在点击本地通知的时候会调用我广播接收的代码。

我在广播的接收中调用handlingNotificationOpen函数。

 private void handlingNotificationOpen(Context context,  Bundle bundle) {
        Log.i(TAG, "----------------  handlingNotificationOpen");

        //AlarmClockCordova是插件中继承CordovaPlugin 的类
        AlarmClockCordova.transmitNotificationOpen();

        Intent launch = context.getPackageManager().getLaunchIntentForPackage(
                context.getPackageName());
        launch.addCategory(Intent.CATEGORY_LAUNCHER);
        launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(launch);
    }

transmitNotificationOpen 要是全局的函数

public class  AlarmClockCordova extends CordovaPlugin {
    private static AlarmClockCordova instance;
    private static Activity cordovaActivity;

   public AlarmClockCordova() {
        instance = this;
    }



    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        cordovaActivity = cordova.getActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cordovaActivity = null;
        instance = null;
    }
   ...

  static void transmitNotificationOpen() {
        if (instance == null) {
            return;
        }
        final String format = "window.plugins.alarmClock.openNotificationInAndroidCallback();";
       // final String js = String.format(format);
        cordovaActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                instance.webView.loadUrl("javascript:" + format);
            }
        });

    }
   ...
}

我们可以看到CordovaPlugin类中是有个CordovaWebView的webveiw,我们直接用这个来调用JS代码就可以了。

public CordovaWebView webView;

主要的代码是webView.loadUrl(“javascript:” + format);这句就是在调用js的函数

js代码

 function AlarmClock() {
  }

 AlarmClock.prototype.openNotificationInAndroidCallback = function () {//cordova.fireDocumentEvent('alarm.openNotification', {})
//你的操作。。。
                          alert("提醒下!");
                        }


    if (!window.plugins) {
      window.plugins = {}
    }

    if (!window.plugins.alarmClock) {
      window.plugins.alarmClock = new AlarmClock()
    }

这样点击本地的消息通知就会蹦出个”提醒下!”的提示。一个简单的小例子。希望对做插件的小伙有点帮助。当然其实原生开发的时候想在里面调用JS的方法,也可以用webView.loadUrl(“javascript:” + method);method是js的function名字。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值