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

How to register and recieve push notification? #47

Open jannomeister opened 5 years ago

jannomeister commented 5 years ago

@roblav96 how to configure this plugin to receive push notification from onesignal? badly needed for my project :(

karlix commented 5 years ago

@jannomeister in the init file (apps.ts):

var TnsOneSignal = require('nativescript-onesignal').TnsOneSignal

if (app.android) {
    app.on(app.launchEvent, function(args: app.ApplicationEventData) {
        try {
            TnsOneSignal.startInit(app.android.context) // init to recieve push notification
                .setNotificationReceivedHandler(new TnsOneSignal.NotificationReceivedHandler({
                    notificationReceived: notification => {
                        // when recive notification
                       ...............
                    }
                }))
                .setNotificationOpenedHandler(new TnsOneSignal.NotificationOpenedHandler({
                    notificationOpened: result => {
                        //when user click on notification
                          ....
                          //in the 'result' variable you have the data related to the notification
                    }
                }))
                .init();
        } catch (error) {
            console.error('error', error)
        }
    })
}

if (app.ios) {
    class MyDelegate extends UIResponder implements UIApplicationDelegate {

        public static ObjCProtocols = [UIApplicationDelegate]

        public applicationDidFinishLaunchingWithOptions(app: UIApplication, launchOptions: NSDictionary<any,any>): boolean {

            try {

                console.log('TnsOneSignal', TnsOneSignal)
                // init to recieve push notification
                TnsOneSignal.initWithLaunchOptionsAppId(launchOptions, 'your_id_key_one_signal');

               // I have not yet implemented the part of receiving notifications in ios, when I have it I will publish it

            } catch (error) {
                console.error('error', error)
            }

            return true
        }

    }
    app.ios.delegate = MyDelegate
}
...
app.run({ moduleName: 'app-root/app-root' });  // your app-root

this is working correctly for me 😉

AntoineBouquet commented 5 years ago

Is there a way to do to use this code in other file than the main project file ? And it is possible to set the One Signal App Id for Android in this piece of code ?

I need to variabilize One Signal App ID getting it somewhere instead of write it directly in the code, and so making the initialization after getting it.

karlix commented 5 years ago

Is there a way to do to use this code in other file than the main project file ? And it is possible to set the One Signal App Id for Android in this piece of code ?

I need to variabilize One Signal App ID getting it somewhere instead of write it directly in the code, and so making the initialization after getting it.

Here what is being done is to link it to the application launch event, so that when the application starts, the OneSignal start code will be executed. But if you run the same start code of OneSignal in some other place, I do not see why it should not work (but I can not assure you that it works, since I have not tried it)

The only problem I see, is that in case of android, the id of onesignal is placed in the project's gradle files and I do not know if it will be possible to change this value in execution time. Check if the onsignal sdk for android has some method to pass the onesignalid in time of execution (as it does for ios)

https://documentation.onesignal.com/docs/android-native-sdk

jannomeister commented 5 years ago

@karlix how will you know in onesignal dashboard that your deviec successfully registered?

karlix commented 5 years ago

@jannomeister In the Users section, subscribed users segment. The list of users subscribed to onesignal appears.

jannomeister commented 5 years ago

@karlix did you successfully implemented how to receive push notifs in iOS? :(

meysam-mahmoodi commented 5 years ago

@karlix did you successfully implemented how to receive push notifs in iOS? :(

could you run the plugin successfully? I have the same issue.

FlawaCLV commented 4 years ago

@karlix or anyone has manage to make it work for iOS ?