前奏:
我正在Ionic 3中构建一个需要配置通知服务的应用程序。现在我知道Ionic Framework提供的Push Notification services但是客户端只需要使用Oracle Push Cloud Service(以前称为Push IO)。 因此,我为Android创建了一个自定义cordova插件,它接收通知并将其传递给相应的Javascript函数,然后我想将这些数据传递给我在Typescript中的主模块。下图解释了项目结构:
以下是我的cordova插件中的NotificationReceiver类的代码。此类接收通知数据:
public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
try {
PushIoUtils.sendPushPayload(intent.getStringExtra("alert"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
以下是我的cordova插件中PushIoUtils类的sendPushPayload函数的代码。此类将通知数据从Receiver传递给plugin.js:
public class PushIoUtils extends CordovaPlugin {
public static CordovaWebView gWebView;
public static String notificationCallBack = "pushio.onNotificationReceived";
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
gWebView = webView;
Log.d("Yes", "Initializing PushIoPlugin");
}
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if(action.equals("init")){
PushIOManager pushIOManager = PushIOManager.getInstance(cordova.getActivity().getWindow().getContext());
try {
String str = "";
try {
str = args.getString(0);
} catch (JSONException ex) {
callbackContext.error("userId does not appear to be a valid String");
return true;
}
pushIOManager.declarePreference("IMEI_NUMBER", "NA", PushIOPreference.Type.STRING);
if (str != null || str.length() > 0) {
pushIOManager.setPreference("IMEI_NUMBER", str);
}
} catch (Exception ex) {
//Log.d("Push Io", "Preferences in not set " + ex.getMessage());
}
}
return true;
}
public static void sendPushPayload(String payload) {
try {
String callBack = "javascript:" + notificationCallBack + "(" + payload + ")";;
gWebView.sendJavascript(callBack);
} catch (Exception e) {
e.printStackTrace();
}
}
}
以下是我的自定义插件中plugin.js的代码,它充当主模块的Native和Typescript类之间的桥梁:
cordova.define("cordova-plugin-pushio.plugin", function(require, exports, module) {
var exec = require('cordova/exec');
function pushio() {
console.log("pushio.js: is created");
}
pushio.prototype.init = function( topic, success, error ){
exec(success, error, "pushio", 'init', [topic]);
}
pushio.prototype.onNotificationReceived = function(payload){
console.log("Received push notification");
console.log(payload);
// Passing data to Typescript class
var c = new MyproviderProvider();
c.getData(payload);
}
var pushio = new pushio();
module.exports = pushio;
});
这里getData是我home.ts文件中的一个函数。我按照here给出的步骤将数据从Javascript函数传递给Typescript类。
挑战:
按照上述步骤传递通知数据,我收到以下错误:
vendor.js:1443 ERROR Error: Uncaught (in promise): ReferenceError: MyproviderProvider is not defined
ReferenceError: MyproviderProvider is not defined
at pushio.onNotificationReceived (plugins/cordova-plugin-pushio/www/plugin.js:21)
at eval (eval at processMessage (cordova.js:1108), <anonymous>:1:19)
at processMessage (cordova.js:1108)
at processMessages (cordova.js:1142)
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:4508)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at pushio.onNotificationReceived (plugins/cordova-plugin-pushio/www/plugin.js:21)
at eval (eval at processMessage (cordova.js:1108), <anonymous>:1:19)
at processMessage (cordova.js:1108)
at processMessages (cordova.js:1142)
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:4508)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at c (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:4499)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
at o (polyfills.js:3)
at <anonymous>