首页 文章

Javascript回调未在Cordova Android应用中触发

提问于
浏览
1

我正在使用Cordova Android应用程序来接收推送通知 . 应用程序没有问题,除了没有任何Javascript回调被触发 . 例如,在下面的代码中,successHandler或errorHandler应该触发......但它们不会触发 .

function successHandler (result) { 
    alert('Success - ' + result); 
}

function errorHandler (error) {
    alert('Success - ' + result);
}

nNotification.register(successHandler, errorHandler, {"senderID":"abcdefghijklmnopn","ecb":"onNotificationGCM"});

对于从Java发送到webview的其他Javascript cide也是如此(例如onNotificationGCM . )

什么可能阻止来自Java的所有Javascript被解雇?

1 回答

  • 0

    在GCM成功注册后,您将获得onNotificationGCM功能的回调

    ({"senderID":"abcdefghijklmnopn","ecb":“ onNotificationGCM ”})所以,添加

    window. onNotificationGCM = function(msg){
      alert(JSON.stringify(msg))
    
    }
    

    你会得到回调,如果GCM注册失败,你将不会得到任何回调 .

    如果需要在successHandler函数上获得回调,则需要编辑一些java文件

    1.GCMBaseIntentService.java

    替换 PushPlugin.sendJavascript( json );

    PushPlugin.sendOnregisterSuccess( json );

    2.PushPlugin.java

    添加一个公共变量

    public static CallbackContext cal= null;
    

    在里面

    public boolean execute(String action, JSONArray data, CallbackContext callbackContext)
    

    功能添加

    cal = callbackContext;
    

    并在 PushPlugin 类中添加新的私有函数

    public static void sendOnregisterSuccess(JSONObject _json) {
            //String _d = "javascript:" + gECB + "(" + _json.toString() + ")";
            //Log.v(TAG, "sendJavascript: " + _d);
            cal.success(_json);
    
        }
    

    现在,您将在 successHandler 函数中获得注册successCallback

相关问题