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

NativeScript angular2 actionCallback navigate #8

Open tushar-1health opened 7 years ago

tushar-1health commented 7 years ago

Hello,

I was able to configure the plugin. However, I am listening to actionCallback to get buttons and route to them. But since angular2 is not yet initiated in main.ts how would it work out? So on click a button depending on id of the button route to a angular2 component.

Regards

tushar-1health commented 7 years ago

@roblav96 Any update?

roblav96 commented 7 years ago

@tushar-1health I have not previously used this feature before so I'm not sure how this would work =/

sonnh58 commented 7 years ago

@tushar-1health i can click notification then navigate to detail page depend on ID of item.

i store notification.payload.additionalData to config then if ID in additionalData is exist, navigate to detail page on onInit function

tusharvikky commented 7 years ago

@sonnh58 I stored additionalData in localstorage and in default route, I checked if it available redirect to it. It works fine in Android. However, in iOS it saved in localstorage but doesn't redirect on first hit on default route component, but If you navigate to a different route and come back to default route it then redirect to the desired page.

Could you please create a gist which can help w/ your oninit function?

sonnh58 commented 7 years ago

@tusharvikky i only store it in a variable.. tap to redirect only work when 1 user are not using app. when they tap message, app is opened

on default router component i check if this variable

ngOnInit(): void { 
if (Config.itemId !=  "") {
                    //go to detail page
                    this.goDetail(Config.itemId);
                }
}
davorpeic commented 7 years ago

In addition to @sonnh58 answer, I used "resume" events available from application, so for example, if you are in some other app chatting, and you get notification and your app is open just in background this will trigger this event. For other cases (when ngOnInit() gets triggered use @sonnh58 way to handle details.

import * as application from "application";

....

application.on(application.resumeEvent, (args) => {
    if (args.android) {
        // For Android applications, args.android is an android activity class.
        console.log("Activity: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is UIApplication.
        console.log("UIApplication: " + args.ios);
    }
});

Taken from docs https://docs.nativescript.org/core-concepts/application-lifecycle#use-application-events and using this in my angular app.

dp

ghost commented 7 years ago

Please @sonnh58 help me with code how can I get additioanal data in app. Many many thanx. My code is:

const TnsOneSignal = require('nativescript-onesignal').TnsOneSignal;

if ( application.ios ) {
            class MyDelegate extends UIResponder implements UIApplicationDelegate {
                public static ObjCProtocols = [UIApplicationDelegate];
                applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<any, any>): boolean {
                    try {
                        //console.log('TnsOneSignal', TnsOneSignal);
                        TnsOneSignal.initWithLaunchOptionsAppId(launchOptions, 'xxxxxxxxxxxxxx');
                    } catch (error) {
                        console.error('error', error);
                    }
                    return true;
                }
                applicationDidBecomeActive(application: UIApplication): void {
                    //console.log("applicationDidBecomeActive: " + application);
                }
            }
            application.ios.delegate = MyDelegate;
        }

        if ( application.android ) {
            application.on(application.launchEvent, function(args: application.ApplicationEventData) {
                try {
                    //console.log('TnsOneSignal', TnsOneSignal);
                    //TnsOneSignal.autoRegister = false;
                    //console.dir(TnsOneSignal);
                    TnsOneSignal.startInit(application.android.context)
                        .unsubscribeWhenNotificationsAreDisabled(true)
                        .init();
                } catch (error) {
                    console.error('error', error)
                }
            });
        }
ghost commented 7 years ago

@tushar-1health or @sonnh58 Can you help me how can I get onesignal message or title or additioanl data in my Nativescript-angular app? Thanx Bumbella

markosole commented 4 years ago

@sonnh58 I've managed to do what you looking for. It's not the best solution but works (in one case does not) Here is example:

if (application.android) {  

            TnsOneSignal.startInit(application.android.context).init();
            TnsOneSignal.init(application.android.context,"" ,"",new
                TnsOneSignal.NotificationOpenedHandler({
                    notificationOpened: function (result) {  
                        var obj = JSON.parse(result.notification.payload.rawPayload); 
                        var obj2 = JSON.parse(obj.custom)  

                        switch(obj2.a.vrsta) {
                                case "kategorija":       
                                appSettings.setString("vrsta", obj2.a.vrsta); 
                                appSettings.setString("vrsta-id", obj2.a.id); 
                                  break;  

                                  case "novost":              
                                  appSettings.setString("vrsta", obj2.a.vrsta); 
                                  appSettings.setString("vrsta-id", obj2.a.id); 
                                  break; 

                                default:
                                  // code block
                        } 
                    },
                })
            ); 
            TnsOneSignal.setSubscription(true);   
            var status=TnsOneSignal.getPermissionSubscriptionState();
            console.log("Player ID is: " + status.getSubscriptionStatus().getUserId()); 

}

As you can see, OneSignal sends keys and you can access them like "obj2.a.YOUR_KEY_NAME". In my example, I am saving data to local storage and root fires up default view (what ever you set there). Than in that view's module create a check for saved data and do what you need to do with it - navigate to other page if ...( appSettings.getString("YOUR_KEY_NAME", "false") === "true") etc.

@davorpeic now.. this doe not work ONLY if app is killed and opened by received notification. Looks like that there is no payload or something else is wrong with my code lol

Can you please have a quick look. Thx! Hvala :)