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

TnsOneSignal.initWithLaunchOptionsAppId is not a function #65

Open markosole opened 3 years ago

markosole commented 3 years ago

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 commented 3 years ago

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

markosole commented 3 years ago

@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 commented 3 years ago

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

markosole commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

You are correct, go give it a try :)

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

elconix commented 3 years ago

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

elconix commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

elconix commented 3 years ago

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 commented 3 years ago

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

markosole commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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

elconix commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

Only now the android issue, did you try it?

Thanks

markosole commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

Ok tried this code, but it only got to Android 1:

if (application.android) {

console.log("Android 1") ;

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()); formData.append('onesignal_id', status.getSubscriptionStatus().getUserId()); // do something with status ID

console.log("Android 2") ;

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

console.log("Android 3") ;

       }

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

}) }

This is android.

El vie, 8 ene 2021 a las 22:04, Marko Solomun (notifications@github.com) escribió:

@elconix https://github.com/elconix Here is android sample (I haven't tested for a year or so) which should give you what you need

` 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?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/roblav96/nativescript-onesignal/issues/65#issuecomment-757085034, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASKWDX2U4U35KO4RF2PMSXDSY7BUZANCNFSM4U4MRKOA .

markosole commented 3 years ago

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 commented 3 years ago

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 notifications@github.com 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 https://github.com/elconix

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/roblav96/nativescript-onesignal/issues/65#issuecomment-757564178, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASKWDX2GJLTFUT77Q5WXNH3SZI25FANCNFSM4U4MRKOA .

elconix commented 3 years ago

Hello Marko any news about android?

On Sun, Jan 10, 2021 at 6:43 PM Moises Vega mvega@grupoenx.com 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 notifications@github.com 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 https://github.com/elconix

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/roblav96/nativescript-onesignal/issues/65#issuecomment-757564178, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASKWDX2GJLTFUT77Q5WXNH3SZI25FANCNFSM4U4MRKOA .

markosole commented 3 years ago

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 commented 3 years ago

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

        "@nativescript/core": "^6.5.19",
markosole commented 3 years ago

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 commented 3 years ago

@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?