0

I´ve been searching a lot to find a solution but my problem as to do with some understanding of the mechanism, so i was wondering if someone could help me:

At this moment i´m sending push notifications using the REST api from OneSignal and it´s working...but i don´t know how to reach the data sent in order to tell the app to go to a specific place:

My server side:

public function sendMessage(){
        $content      = array(
            "en" => 'Nova Angariação'
        );
        $hashes_array = array();
        array_push($hashes_array, array(
            "id" => "like-button",
            "text" => "Ver",
            "icon" => "http://i.imgur.com/N8SN8ZS.png",
            "url" => "https://yoursite.com"
        ));
        $fields = array(
            'app_id' => "myAppId",
            'included_segments' => array(
                'All'
            ),
            'data' => array(
                "foo" => "bar"
            ),
            'contents' => $content,
            'buttons' => $hashes_array
        );
        $fields = json_encode($fields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Authorization: Basic my rest key'
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $resp = curl_exec($ch);
        curl_close($ch);
        return $resp;
    }

Now in my app(i´m using nativescript with OneSignal SDK):

if (application.ios) {
    const MyDelegate = /** @class */ (function (_super) {
        __extends(MyDelegate, _super);
        function MyDelegate() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        MyDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (app, launchOptions) {
            try {

                **//i think it´s here where i have to handle the handleNotificationOpened but i don´t know how**

                TnsOneSignal.initWithLaunchOptionsAppId(launchOptions, 'my onesignal app key'); 
            }
            catch (error) {
                console.error('error', error);
            }
            return true;
        };
        MyDelegate.ObjCProtocols = [UIApplicationDelegate];
        return MyDelegate;
    }(UIResponder));

    application.ios.delegate = MyDelegate;
}

How do i do this?

Thanks for your time Regards.

Japa
  • 632
  • 7
  • 30
  • You may be interested in [this answer](https://stackoverflow.com/a/16393957/2708650) about handling push notifications received (in the background). Just another addition to your delegate. – Ian MacDonald Oct 26 '18 at 14:51
  • Thanks for answering Ian, but that´s objective C syntax...how do you convert that in javascript to my nativescript app?, actually that´s a really old code..onesignal provides it in swift and i still don´t know how to convert that into my app – Japa Oct 26 '18 at 15:13
  • The signature for the [method you implemented](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application?language=objc) should provide some clues here. There is a `*ios.d.ts` in the NativeScript repos (somewhere) that provides the description if you have trouble. They're pretty good about just mapping the methods as close to the native signature as they can. – Ian MacDonald Oct 26 '18 at 15:23

1 Answers1

0

If you want to access the notification data upon app launch, then try this inside applicationDidFinishLaunchingWithOptions

if (launchOptions) {
  const userInfo = launchOptions.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey);
  if (userInfo) {
     const aps = userInfo.objectForKey("aps");
     if (aps !== null) {
       // aps will be again a dictionary will have all data for the notification
     }
  }
}

Between didReceiveRemoteNotification also works, but when application is already in foreground.

Manoj
  • 21,753
  • 3
  • 20
  • 41