Nativescript с push-уведомлениями OneSignal - ПОЛУЧИТЕ данные в приложении - PullRequest
0 голосов
/ 26 октября 2018

Я много искал, чтобы найти решение, но моя проблема связана с некоторым пониманием механизма, поэтому мне было интересно, может ли кто-нибудь мне помочь:

В данный момент я отправляюpush-уведомления , использующие API REST от OneSignal, и он работает ... но я не знаю, как добраться до данных , отправленных для того, чтобы приложение перешло кконкретное место:

Моя сторона сервера:

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;
    }

Сейчас в моем приложении (я использую nativescript с 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;
}

Как мне это сделать?

Спасибо за ваше время С уважением.

1 Ответ

0 голосов
/ 26 октября 2018

Если вы хотите получить доступ к данным уведомления при запуске приложения, попробуйте это внутри 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
     }
  }
}

Между didReceiveRemoteNotification также работает, но когда приложение уже на переднем плане.

...