roblav96 / nativescript-onesignal

A Nativescript plugin that wraps the iOS and Android OneSignal Push Notifications SDK.
https://documentation.onesignal.com/docs/getting-started
Other
24 stars 42 forks source link

TS2420: Class 'MyDelegate' incorrectly implements interface 'UIApplicationDelegate' #63

Open markosole opened 4 years ago

markosole commented 4 years ago

Hi guys,

I am trying to setup OneSignal push notification on iOS13, with Xcode 11 (if it matter) and I can't make it work on iOS. I have created JS code for Android and it works, but iOS is more complex and I could not make it work properly. I've downloaded Demo app, changed App ID and OneSignal key to reflect what I'm using in real app but I am getting same errors. (check it below)

image

I am not sure if this is related to my Xcode version, dev tools etc. I am looking for JS version of code, if anyone can help with that please.

Thanks!

Atypic-prod commented 4 years ago

Hi, here is my JS version (in app.js) for onesignal. Tested with iOS 13 & XCode 11.

const application = require("tns-core-modules/application");
const TnsOneSignal = require("nativescript-onesignal").TnsOneSignal;

if (application.ios) {
    const MyDelegate = (function(_super) {
        __extends(MyDelegate, _super);
        function MyDelegate() {
            _super.apply(this, arguments);
        }
        MyDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {
            console.log("applicationWillFinishLaunchingWithOptions: " + launchOptions);
            try {
                console.log('TnsOneSignal', TnsOneSignal);
                TnsOneSignal.initWithLaunchOptionsAppId(launchOptions, "your-app-id-from-onesignal");
            } catch (error) {
                console.log("Error: ", error)
            }
            return true;
        };
        MyDelegate.prototype.applicationDidBecomeActive = function (application) {
            console.log("applicationDidBecomeActive: " + application);
        };
        MyDelegate.ObjCProtocols = [UIApplicationDelegate];
        return MyDelegate;
    })(UIResponder);

    application.ios.delegate = MyDelegate;
    application.run({ moduleName: "app-root" });

} else if (application.android) {
    application.on(application.launchEvent, function(args) {
        try {
            console.log("TnsOneSignal: ", TnsOneSignal);
            TnsOneSignal.startInit(application.android.context).init();
        } catch (error) {
            console.log("Error: ", error);
        }
    });
    application.run({ moduleName: "app-root" });

}
markosole commented 4 years ago

Hi LaurentP, thanks for sharing this. I will try it now.

Just one question related to "Apns Delegate Never Fired" or "Other Apns 3000 Error" errors. Did you receive any of them? If yes, what steps did you take to resolve them.

I have tried another plugin, nativescript-onesignal-sdk which was giving me those errors. One of build worked eventually but after platform removal it stopped. :/

UPDATE: Solution above actually worked. For anyone having problems with Apns, remember to remove platform and add it back again. It's related to existing pods from previous build so keep in mind to clean it.

markosole commented 3 years ago

Hi, here is my JS version (in app.js) for onesignal. Tested with iOS 13 & XCode 11.

const application = require("tns-core-modules/application");
const TnsOneSignal = require("nativescript-onesignal").TnsOneSignal;

if (application.ios) {
    const MyDelegate = (function(_super) {
        __extends(MyDelegate, _super);
        function MyDelegate() {
            _super.apply(this, arguments);
        }
        MyDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {
            console.log("applicationWillFinishLaunchingWithOptions: " + launchOptions);
            try {
                console.log('TnsOneSignal', TnsOneSignal);
                TnsOneSignal.initWithLaunchOptionsAppId(launchOptions, "your-app-id-from-onesignal");
            } catch (error) {
                console.log("Error: ", error)
            }
            return true;
        };
        MyDelegate.prototype.applicationDidBecomeActive = function (application) {
            console.log("applicationDidBecomeActive: " + application);
        };
        MyDelegate.ObjCProtocols = [UIApplicationDelegate];
        return MyDelegate;
    })(UIResponder);

    application.ios.delegate = MyDelegate;
    application.run({ moduleName: "app-root" });

} else if (application.android) {
    application.on(application.launchEvent, function(args) {
      try {
          console.log("TnsOneSignal: ", TnsOneSignal);
          TnsOneSignal.startInit(application.android.context).init();
      } catch (error) {
          console.log("Error: ", error);
      }
  });
    application.run({ moduleName: "app-root" });

}

Hi @Atypic-prod Have you tried this on NS 7 and build for iOS 14? - Looks like it does not work, I am getting error for TnsOneSignal.initWithLaunchOptionsAppId saying that it's not a function.

Atypic-prod commented 3 years ago

Hello, Sorry but no, I don't test this on iOS 14 because I don't use Nativescript anymore... This repo doesn't seem to be updated for NS7 and iOS 14, and official Onesignal SDK's for native push notifications doesn't exist except for Cordova/React-native/Xamarin/Unity and of course native iOS and Android. Honnestly, if I have an application made with NS and the need to do push notifications today, I will take the time to port it to another solution (like react-native) with official support from Onesignal.

markosole commented 3 years ago

@Atypic-prod thanks for reply. Yeah sadly there is no support for NS any more (not available on onesignal website). I will try to use native way to implement on iOS.

Nativescript is sinking down deep :/

I am looking for help to implement it on Native level, if anyone has experience with such a thing. I did follow OneSignal SDK for iOS and still working on it (do not know where to add code from step 5 described here: https://documentation.onesignal.com/docs/ios-sdk-setup

Navigate to your AppDelegate file and add the OneSignal initialization code

But where is AppDelegate.. hm...

Atypic-prod commented 3 years ago

@markosole Before following the Onesignal iOS SDK setup, you need to have a native app project first. After, you can create in that project a Notification Service Extension. Depending on your choice you'll have an AppDelegate.swift or AppDelegate.m at the root of your project, the place for adding the Onesignal initialization code! Good luck!

markosole commented 3 years ago

I will keep trying, it is not simple as it sounds. Switching to another solution like Flutter makes more sense now. I did use Intel's XDK before and Cordova, but every solution has it's flows (as this one right now). If I make it work I'll share the code and how-to.

If anyone can give a hand I would appreciate it.

markosole commented 3 years ago

@Atypic-prod I've made it work and you can check out my repo for forked version. I have not changes anything there yet, just added new instructions how to use.