Ole Haugset
Ole Haugset

Reputation: 3797

OneSignal for Android in NativeScript. Wont load library

I'm trying to implement OneSignal Push Notifications in my NativeScript application for both Android and iOS. For iOS I got it working just fine with CocoaPods. However, when I am trying to compile the OneSignal package for Android, I cant get access to the com.onesignal.

So first off, I've added this to my App_Resources/Android/App.gradle:

dependencies {
    compile 'com.onesignal:OneSignal:3.+@aar'
    compile 'com.google.android.gms:play-services-gcm:+'
    compile "com.google.android.gms:play-services-location:+"
}

android {  
  defaultConfig {  
    generatedDensities = []  
    applicationId = "<HIDDEN>"  
    multiDexEnabled true
    manifestPlaceholders = [manifestApplicationId: "<HIDDEN>", onesignal_app_id: "<HIDDEN>", onesignal_google_project_number: "<HIDDEN>"]
  }  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
} 

I've the run the command tns build android This gives me this line: :prepareComOnesignalOneSignal311Library UP-TO-DATE

My app.js looks like this:

var application = require("application");

application.on(application.launchEvent, function (args) {
    if (args.android) {
        console.dump(com.onesignal.OneSignal);
    } 
});

// iOS specific configuration
if (application.ios) {

    var __extends = this.__extends || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };

    var appDelegate = (function (_super) {
       __extends(appDelegate, _super);
       function appDelegate() {
           _super.apply(this, arguments);
       }

       appDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {
            OneSignal.initWithLaunchOptionsAppId(launchOptions, '<HIDDEN>');
            //console.dump(OneSignal);
            OneSignal.sendTagValue('route', 599);
       };

       appDelegate.ObjCProtocols = [UIApplicationDelegate];
       return appDelegate;

    })(UIResponder);

   application.ios.delegate = appDelegate;

}

application.start({ moduleName: "main-page" });

When running the app the emulator just gives me a white screen with the following, telling me that com.onesignal is null:

Unhandled Exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{no.trv.tommeplan/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
Calling js method onCreate failed

TypeError: Cannot read property ‘OneSignal’ of undefined
File: «/data/data/no.trv.tommeplan/files/app/app.js, line: 5, column:31...

So basically, when dumping com the object only contains com.android, com.app and com.tns. So onesignal isn't imported it seems like. Now, Im stuck. Anyone got any ideas?

Upvotes: 1

Views: 330

Answers (1)

Brad Martin
Brad Martin

Reputation: 6147

Looks like you've done everything correctly. However, my guess is that you forgot something a lot of people overlook :) If you have built & ran this .apk on the device/emulator prior to adding the lib in app.gradle there is a chance you aren't actually getting the updated .apk on the device/emulator. I don't know why this happens on Android, it's some sort of caching I suppose, never looked into it too much.

So what you need to do is outlined here: https://bradmartin.net/2016/07/20/ahhh-this-nativescript-plugin-doesnt-work/ and I'll also list the steps here.

  1. Uninstall the .apk from the device/emulator
  2. Execute tns build android to get a fresh .apk (just to be certain)
  3. Execute tns run android or tns livesync android which will install the updated .apk on the device/emulator.
  4. Get a beer (hopefully).

Seriously, that should resolve the issue if indeed the library is installing correctly.

Upvotes: 1

Related Questions