How to integrate hockey App with Hybrid mobile app

*爱你&永不变心* 提交于 2019-12-04 12:23:21

Main activity is inside Android platform... cordova/platforms/android/src/...

Put in onCreate method the Register...

There also some plugins for help in this task like https://github.com/peutetre/cordova-plugin-hockeyapp

Take into account that a lot of crash JavaScript problems do not crash in native world it would be helpful to use additional way to communicate controlled errors for example the saveException method, try to expose this by plugin into javascript, it will let store context information error: http://hockeyapp.net/help/sdk/android/3.0.1/net/hockeyapp/android/ExceptionHandler.html

I have tested the solution for Android only in a fork of the previous mentioned plugin: https://github.com/m-alcu/cordova-plugin-hockeyapp

There are several actions available but yo only need to use "start" and "saveException" for controlled errors to be send to hockeyapps.

hockeyapp.js:

var exec = require('cordova/exec');

var hockeyapp = {
    start:function(success, failure, token) {
        exec(success, failure, "HockeyApp", "start", [ token ]);
    },
    feedback:function(success, failure) {
        exec(success, failure, "HockeyApp", "feedback", []);
    },
    saveException:function(success, failure, description) {
        exec(success, failure, "HockeyApp", "saveException", [ description ]);
    }    
};

module.exports = hockeyapp;

hockeyapp.java:

package com.zengularity.cordova.hockeyapp;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import android.widget.Toast;

import static net.hockeyapp.android.ExceptionHandler.saveException;
import net.hockeyapp.android.FeedbackManager;
import net.hockeyapp.android.CrashManager;
import net.hockeyapp.android.CrashManagerListener;

public class HockeyApp extends CordovaPlugin {

    public static boolean initialized = false;
    public static String token;
    public static String description;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        if (action.equals("start")) {
            token = args.optString(0);
            CrashManager.register(cordova.getActivity(), token, null);
            initialized = true;
            callbackContext.success();
            return true;
        } else if(action.equals("feedback")) {
            token = args.optString(0);
            FeedbackManager.register(cordova.getActivity(), token, null);
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    FeedbackManager.showFeedbackActivity(cordova.getActivity());
                }
            });
            callbackContext.success();
            return true;

        } else if(action.equals("saveException")) {
            description = args.optString(0);
            if(initialized) {

            Toast toast = Toast.makeText(cordova.getActivity(), "problem", Toast.LENGTH_SHORT);
            toast.show();

            cordova.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Exception e = new Exception("Send problem");
                        saveException(e, new CrashManagerListener() {
                            public String getDescription() {
                                return description;
                            }
                        });
                    }
                });
                callbackContext.success();
                return true;
            } else {
                callbackContext.error("cordova hockeyapp plugin not initialized, call start() first");
                return false;                
            }  
        }
        else {
            return false;
        }
    }

}

example of use this plugin in a hellowold example (index.js):

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);

        hockeyapp.start(
            function() { alert('hockeyapp initialised'); },
            function(msg) { alert(msg); },
            '< your APP ID >');

        hockeyapp.saveException(
            function() { alert('hockeyapp saveException'); },
            function(msg) { alert(msg); },
            'Something wrong has happened: bla bla bla...');    
    }
};

app.initialize();

Hockey stores these controlled exceptions in the file directory of the app and asks to send it the next time user opens app:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!