1

Prelude:

I am building an application in Ionic 3 which needs to be provisioned with a notification service. Now I am aware of the Push Notification services provided by Ionic Framework however it's the client's requirement to use Oracle Push Cloud Service (formerly Push IO) only. Therefore, I created a custom cordova plugin for Android which receives the notification and passes it to the respective Javascript function and thereafter I want to pass this data to my main module which is in Typescript. Below diagram explains the project structure:

enter image description here

Below is the code for my NotificationReceiver class within my cordova plugin. This class receives the Notification data:

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();
       }
    }
  }
}

Below is the code for sendPushPayload function of PushIoUtils class within my cordova plugin. This class passes the Notification data from the Receiver to the 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();
        }
  }
}

Below is the code for plugin.js within my custom plugin which acts as a bridge between Native and Typescript classes of the main module:

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;
});

Here getData is a function in my home.ts file. I am following the steps given here to pass data from Javascript function to Typescript class.

Challenge:

On following the above mentioned steps to pass the Notification data, I get the below error:

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>
Sanket Mendon
  • 403
  • 5
  • 11
  • 1
    typescript is transpiled to javascript, you just need to properly determine what the outputed js will be for your code. :) – toskv Oct 03 '17 at 11:10
  • @toskv Thank you very much for your early response. One question...are you saying that I will have to instantiate MyproviderProvider class and call its getData function all within my plugin.js? In other words, write all that's within pushio.prototype.onNotificationReceived function in a javascript notation? – Sanket Mendon Oct 04 '17 at 05:28

0 Answers0