具有OneSignal推送通知的Nativescript-在应用程序上获取数据

时间:2018-10-26 14:46:36

标签: push-notification nativescript onesignal

我一直在寻找很多解决方案,但是我的问题与对机制的某种理解有关,所以我想知道是否有人可以帮助我:

此刻,我正在使用OneSignal的REST API 发送推送通知,它正在工作...但是我不知道如何到达数据发送以告诉应用程序转到特定位置:

我的服务器端:

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

现在在我的应用程序中(我在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 :(得分:0)

如果您想在应用启动时访问通知数据,请在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之间也可以,但是当应用程序已经在前台时。