Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TnsOneSignal.initWithLaunchOptionsAppId is not a function #65

Open
markosole opened this issue Dec 15, 2020 · 28 comments
Open

TnsOneSignal.initWithLaunchOptionsAppId is not a function #65

markosole opened this issue Dec 15, 2020 · 28 comments

Comments

@markosole
Copy link

markosole commented Dec 15, 2020

Hi all,

I have upgraded my tns to ns 7 and plugin isn't working any more properly for iOS 14.x . I am getting error:
TnsOneSignal.initWithLaunchOptionsAppId is not a function

My code is:

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,"xxxxxxx-xxxxxxx-xxxxxxxxx-xxxxxxxxx");

    } catch (error) {
        console.log("Error: ", error);
    }

    return true;
    };

    MyDelegate.prototype.applicationDidBecomeActive = function (application) {
    console.log("iOS aplikacija je aktivirana: " + application);
    };

    MyDelegate.ObjCProtocols = [UIApplicationDelegate];

    return MyDelegate;
})(UIResponder);
application.ios.delegate = MyDelegate;
}

This was working fine and now it does not any more. Did anyone had same or similar problem?

@elconix
Copy link

elconix commented Jan 3, 2021

Yes same issues, searched for answers and no luck, hope the solution is provided here:

@markosole
Copy link
Author

@elconix I am still looking for a way to implement it. Have you found anything what works? I am on the half way to implement this https://documentation.onesignal.com/docs/ios-sdk-setup as native (if possible). Having trouble adding last piece of puzzle from Step 5 - Add the OneSignal Initialization Code

If anyone knows where and how to add this, it would help a lot. Thc

@elconix
Copy link

elconix commented Jan 6, 2021

Hello @markosole all nativescript plugins suffer the same thing, so mark this will be a new plugin?

@markosole
Copy link
Author

markosole commented Jan 6, 2021

@elconix
I have some good news for you.

Code below works - I can see phone registering on OneSignal. There is one minor problem, OneSignal shows Other Apns 3000 error which I have to fix. That should not be a problem.

My code:

 //app.js
var 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('Init onesignal started...');
            console.log("applicationWillFinishLaunchingWithOptions: " + launchOptions);

            try {
                console.log('Onesignal pokretanje: ', TnsOneSignal);
                console.log('###################################################################################');
                TnsOneSignal.initWithLaunchOptions({ kOSSettingsKeyAutoPrompt: true, kOSSettingsKeyInAppLaunchURL: true, kOSSettingsKeyInFocusDisplayOption: 2, kOSSettingsKeyProvidesAppNotificationSettings: true });

                TnsOneSignal.setAppId("xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx");
                TnsOneSignal.init({
                    kOSSettingsKeyAutoPrompt: true,
                });
            } catch (error) {
                console.log("Error: ", error)
            }
            return true;
        };
        MyDelegate.prototype.applicationDidBecomeActive = function (application) {
            console.log("iOS aplikacija je aktivirana: " + application);
        };
        MyDelegate.ObjCProtocols = [UIApplicationDelegate];

        return MyDelegate;
    })(UIResponder);

    application.ios.delegate = MyDelegate;
    }

What I have noticed, OneSignal have changed the way of accepting parameters, therefore TnsOneSignal.initWithLaunchOptionsAppId can not be used. The AppId and Parameters are now sent separately, as in my example above. Another thing is number of parameters and used in: TnsOneSignal.initWithLaunchOptions({ kOSSettingsKeyAutoPrompt: true, kOSSettingsKeyInAppLaunchURL: true, kOSSettingsKeyInFocusDisplayOption: 2, kOSSettingsKeyProvidesAppNotificationSettings: true });

There are some more added there as iOS 13+ requires that. Check screenshot below. As said before, functions can be added and I will fork to and add those changes once I solve APN problem.

image

EDIT; for your previous question - if you migrated your project to NS7 (which I believe you had to if want to publish for ios 14) than all plugins must be updated. It took me a week to get it all sorted (the ones I am using).

Let's fix OneSignal first and I will try to help with others if can.

@elconix
Copy link

elconix commented Jan 6, 2021

Wow @markosole awsome news, so steps:
1- Add JS code in app.ios.js (I have separate app.js for each platform)
2- Replace with our app id,
3- The screenshot I did not understand very well, what should we do for this?

@markosole
Copy link
Author

markosole commented Jan 6, 2021

You are correct, go give it a try :)

    1. correct add code I've pasted above to your app.ios.js. Do not forget to include const application = require(@nativescript/core).Application;, that is new way of including it and replaces tns-core-modules/application
    1. Add your App ID instead xxxxxx-xxxxxx-xxxxxxx-xxxxx
    1. Screen shot is for reference only for those who are looking for more info about OneSignal SDK changes.

I am working on APN 3000 error. You try and let me know how it goes for you.

@elconix
Copy link

elconix commented Jan 6, 2021

@markosole I use pure js, so it will be this code you attached right?

@elconix
Copy link

elconix commented Jan 6, 2021

Just for your refference:

CONSOLE LOG file: app/app.ios.js:50:0: Init onesignal started...
CONSOLE LOG file: app/app.ios.js:51:0: applicationWillFinishLaunchingWithOptions: null
CONSOLE LOG file: app/app.ios.js:54:0: Onesignal pokretanje:  function OneSignal() {
[native code]
}
CONSOLE LOG file: app/app.ios.js:55:0: ###################################################################################
CONSOLE LOG file: app/app.ios.js:63:0: Error:  Error: Actual arguments count: "1". Expected: "0". (evaluating 'TnsOneSignal.init({
kOSSettingsKeyAutoPrompt: true,
})')

I suppose it initialize ok.

@elconix
Copy link

elconix commented Jan 6, 2021

OK no the next step is to get the person id, for that in my login procedure I added this code:

var onesignal_status=TnsOneSignal.getPermissionSubscriptionState(); formData.append('onesignal_id', onesignal_status.getSubscriptionStatus().getUserId());

Got this in console:
JavaScript error: file: app/screens/login/login-view-model.js:51:0: JS ERROR TypeError: TnsOneSignal.getPermissionSubscriptionState is not a function. (In 'TnsOneSignal.getPermissionSubscriptionState()', 'TnsOneSignal.getPermissionSubscriptionState' is undefined) NativeScript caught signal 11.

Is there a new way to get the person id, or is not yet implemented? O need to update the onesignal-plugin?

@elconix
Copy link

elconix commented Jan 6, 2021

Just as a good sign, I do see the Iphone Simulator in the Onesignal dashboard.

@elconix
Copy link

elconix commented Jan 6, 2021

Ok @markosole worked to compile well in ios, change my old code to:

				if(TnsOneSignal.getDeviceState()) {
					console.log('Onesignal personid: ', TnsOneSignal.getDeviceState().userId);
					formData.append('onesignal_id', TnsOneSignal.getDeviceState().userId);


				 }

the only issue now is that this code does not work for android and also the TnsOneSignal.getPermissionSubscriptionState does not work.

@elconix
Copy link

elconix commented Jan 6, 2021

Do i need to change something in android? the error in android now is:
System.err: TypeError: TnsOneSignal.getDeviceState is not a function

@markosole
Copy link
Author

Just as a good sign, I do see the Iphone Simulator in the Onesignal dashboard.

Yeah but what does it say? "Never Prompted" or all looks okey? - I am getting this "Never Prompted" message and still trying to solve it.

Android should be fine, no need to change anything. To be honest I have not try yet and iOS gives trouble.

@markosole
Copy link
Author

markosole commented Jan 7, 2021

@elconix Good news again;

Here is final example which works and Device is receiving push notifications properly. No fancy stuff haven't been added, you can check out links in comments for more details and available parameters. I can help with implementing them.

As you already probably know, you have to enable background notifications in Xcode project.

// Onesignal SDK Reference: https://documentation.onesignal.com/docs/sdk-reference#setloglevel-method
// Onesignal SDK available parameters: https://documentation.onesignal.com/docs/ios-native-sdk

if (application.ios) {
    const MyDelegate = (function (_super) {
        __extends(MyDelegate, _super);
        function MyDelegate() {
            _super.apply(this, arguments);
        }
        MyDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {

            console.log("Application data: " + application);
            try {
                console.log('Onesignal starts here:', TnsOneSignal);
                TnsOneSignal.initWithLaunchOptions({ kOSSettingsKeyAutoPrompt: true, kOSSettingsKeyInAppLaunchURL: true, kOSSettingsKeyInFocusDisplayOption: 2, kOSSettingsKeyProvidesAppNotificationSettings: true });
                TnsOneSignal.setAppId("xxxxxxx-xxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxxxxxxxx");

                // It is important to call this for any user response. Default iOS option (allow and deny) will be provided. No need for any fancy stuff
                // funCallback is callback function and it wont work without it
                TnsOneSignal.promptForPushNotificationsWithUserResponse(funCallback());
                function funCallback(permission) {
                    console.log("Permission selection callback:" + permission);
                }

            } catch (error) {
                console.log("Error: ", error)
            }
            return true;
        };
        MyDelegate.prototype.applicationDidBecomeActive = function (application) {
            console.log("Application is activated: " + application);
        };
        MyDelegate.ObjCProtocols = [UIApplicationDelegate];

        return MyDelegate;
    })(UIResponder);

    application.ios.delegate = MyDelegate;
}

For the reference, here are typings available in this nativescript plugin: https://github.com/roblav96/nativescript-onesignal/blob/master/typings/OneSignal.ios.d.ts

Check the line starting with declare class OneSignal extends NSObject {

What ever is available here: https://documentation.onesignal.com/docs/ios-native-sdk may not be available in OneSignal.ios.d.ts. OneSignal has updated Parameter list and names, implementation as well.

Forked version: https://github.com/markosole/nativescript-onesignal

@elconix
Copy link

elconix commented Jan 7, 2021

this works with android @markosole have you tested in both platforms?

@elconix
Copy link

elconix commented Jan 7, 2021

Ok @markosole reinstalled the plugin as the link https://github.com/markosole/nativescript-onesignal
Tested in simulator looks fine, waiting for testflight to publish the version for trying in device.

Android does not work, used the documentation you have in the link: https://github.com/markosole/nativescript-onesignal and it sends the following error when asking for the same code working in ios for person id:

System.err: TypeError: TnsOneSignal.getDeviceState is not a function

Let me know how your android compilation works out.

Thanks

@elconix
Copy link

elconix commented Jan 7, 2021

THis is my code for getting person id:

				// OneSignal
				if(application.ios){
					if(TnsOneSignal.getDeviceState()) {
						console.log('Onesignal personid: ', TnsOneSignal.getDeviceState().userId);
					 }
				}
				
				if(application.android){
					if(TnsOneSignal.getDeviceState().isSubscribed()) {
						console.log('Onesignal personid: ', TnsOneSignal.getDeviceState().getUserId());
					}
				}

Ios working, android sending the error:
System.err: TypeError: TnsOneSignal.getDeviceState is not a function

@elconix
Copy link

elconix commented Jan 7, 2021

Ok @markosole awsome, IOS finally working in device and everything ok.

Only now the android issue, did you try it?

Thanks

@markosole
Copy link
Author

What issue you have? Example for Android I've posted works fine. I see you are using getDeviceState, there is something different for getting state. I will have a look into one of mine older projects. Let me know If you still have problem with it.

@markosole
Copy link
Author

markosole commented Jan 9, 2021

@elconix Here is android sample (I haven't tested for a year or so) which should work. The part you need is status.getSubscriptionStatus().getUserId());

The parameter/class/function TnsOneSignal.getDeviceState does not exists in Android, it's. Onesignal Android SDK is using different methods and they have different names too.

var TnsOneSignal = require("nativescript-onesignal").TnsOneSignal;
if (application.android) {
    application.on(application.launchEvent, function (args) {
        try {
            //console.log('TnsOneSignal', TnsOneSignal);         
            if (appSetting.NotificationsEnabled()) {
                TnsOneSignal.startInit(application.android.context).init();
                TnsOneSignal.setSubscription(true);

                // ID OneSignal
                var status = TnsOneSignal.getPermissionSubscriptionState();
                console.log("Andoid push ID: " + status.getSubscriptionStatus().getUserId());

                // do something with status ID

            } else {
                TnsOneSignal.startInit(application.android.context).init();
                TnsOneSignal.setSubscription(false);
            }

            // Sunscribe
            // # TnsOneSignal.startInit(application.android.context).init();
        } catch (error) {
            console.log('error', error);
        }
    })
}

Device state or subscription status, what exactly u need?

@elconix
Copy link

elconix commented Jan 9, 2021 via email

@markosole
Copy link
Author

Sorry for late reply, I will test it ASAP when get chance. For now, remove appSetting.NotificationsEnabled() part as that's something what I built in for checking phone settings and opt-in / out option. You can replace it with if(1==1).... just for test.
I'll fire up my PC in 2-3 days and test on Android. @elconix

@elconix
Copy link

elconix commented Jan 10, 2021 via email

@elconix
Copy link

elconix commented Jan 21, 2021 via email

@markosole
Copy link
Author

markosole commented Jan 21, 2021

Hello Marko any news about android?

On Sun, Jan 10, 2021 at 6:43 PM Moises Vega @.> wrote: Thanks I will wait, as I have a apk working before the ios changes, Thanks On Sun, Jan 10, 2021 at 6:37 PM Marko Solomun @.> wrote: > Sorry for late reply, I will test it ASAP when get chance. For now, > remove appSetting.NotificationsEnabled() part as that's something what I > built in for checking phone settings and opt-in / out option. You can > replace it with if(1==1).... just for test. > I'll fire up my PC in 2-3 days and test on Android. @elconix

Hi @elconix sorry I did not have time to try it. It's def on my list to do as I have to update android versions too. I'm stuck on other project right now. I'll be back here as soon as I get chance, I did not forget about u.

@sido420
Copy link

sido420 commented Jan 29, 2021

Does this problem exist in NS 7+ only? I am on

        "@nativescript/core": "^6.5.19",

@markosole
Copy link
Author

It's same thing starting from 6.5 and newer. It's mainly problem in new SDK and plugin haven't been updated to fulfill those updates / changes. Do you have problem with Android as well?

@elconix I still did not have chance to try. I have two apps waiting to be updated.

@sido420
Copy link

sido420 commented Jan 29, 2021

@markosole
I haven't tried the plugin on any apps yet but was exploring the options on how to use Onesignal with Nativescript.

However, I will be starting with Android so I also will require a fix.

Are there other options then this plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants