Post token device in webservice, using Nativescript

时间:2018-07-24 10:14:28

标签: typescript post nativescript angular2-nativescript

Please can you share with me any idea, how to post in webservice and token device that generate from onRegisterButtonTap(){}.

Like below you can see my code component. I want to add in this code and token device.

   register() {
    let newRegister = this.RegisterForm.value
    let user = new User(newRegister);
    this.registerservice.mobile_registergetdata(user).subscribe(
        result => {
            console.log(result)
            if (result === true) {
                this.router.navigate(['/main']);
            } else {
                this.areWeWaiting = false;
            }
        });
}

Service Component :

  public mobile_registergetdata(user: User): Observable<boolean> {

    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let body = user.generateUrlencodedParameters();
    return this.http.post(Api.getUrl(Api.URLS.mobile_registergetdata), body, {
      headers: headers
    })
      .pipe(map((response: Response) => {
        let res = response.json();
        console.log('res')
        console.log(res)
        if (res.StatusCode === 0) {
          this.router.navigate(['/']);
             return true;
         } else {
          return true;
        }
      }));
  }

And my User Class is like below:

export class User {
  username: string;
  password: string;
  token_device: string;

  constructor(obj?: any) {
    if (obj) {
      this.username= obj && obj.username;
      this.password = obj && obj.password;
      this.token_device= obj && obj.token_device;
    }
  }

  public generateUrlencodedParameters(): string {
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('username', this.username.toString());
    urlSearchParams.append('password', this.password.toString());
    urlSearchParams.append('token_device', this.token_device.toString());

    return urlSearchParams.toString();
  }}

Like below you can see my code Component.

 export class RegisterComponent implements OnInit {
    RegisterForm: FormGroup;
    loading: boolean = false;
    areWeWaiting = false;
    invalidInputs: boolean = false;
    constructor(private formBuilder: FormBuilder, private registerservice: RegisterUserService, private router: Router, private routerExtensions: RouterExtensions) {
        this.RegisterForm = this.formBuilder.group({
            username: ["", Validators.required],
            password: ["", Validators.required]
        });
    }
       register() {
        let newRegister = this.RegisterForm.value
        let user = new User(newRegister);
        this.registerservice.mobile_registergetdata(user).subscribe(
            result => {
                console.log(result)
                if (result === true) {
                    this.router.navigate(['/main']);
                } else {
                    this.areWeWaiting = false;
                }
            });
    }
    public goBack() {
        this.routerExtensions.backToPreviousPage();
    }

    private pushSettings = {
        // Android settings
        senderID: "610353931028", // Android: Required setting with the sender/project number
        notificationCallbackAndroid: (stringifiedData: String, fcmNotification: any) => {
            const notificationBody = fcmNotification && fcmNotification.getBody();
            console.log(notificationBody)
            this.updateMessage("Message received!\n" + notificationBody + "\n" + stringifiedData);
        },
    };
    private _message: string;
    get message(): string {
        return this._message;
    }
    set message(value: string) {
        if (this._message !== value) {
            this._message = value;
            //this.notifyPropertyChange("message", value);
        }
    }

    onCheckButtonTap() {
        let self = this;
        pushPlugin.areNotificationsEnabled((areEnabled: Boolean) => {
            self.updateMessage("Are Notifications enabled: " + !!areEnabled);
        });
    }

    onRegisterButtonTap() {
        let self = this;
        pushPlugin.register(this.pushSettings, (token: String) => {
            self.updateMessage("Device registered. Access token: " + token);
            console.log('"Device registered. Access token: " + token')
            console.log("Device registered. Access token: " + token)
            if (pushPlugin.registerUserNotificationSettings) {
                pushPlugin.registerUserNotificationSettings(() => {
                    self.updateMessage("Successfully registered for interactive push.");
                }, (err) => {
                    self.updateMessage("Error registering for interactive push: " + JSON.stringify(err));

                });
            }
        }, (errorMessage: String) => {
            self.updateMessage(JSON.stringify(errorMessage));
        });
    }

    private updateMessage(text: String) {
        this.message += text + "\n";
    }
}

Any idea please?

Thank you so much.

1 个答案:

答案 0 :(得分:1)

尝试一下:

    register() {
        let self = this;
        pushPlugin.register(this.pushSettings, (token) => {
        self.updateMessage("Device registered. Access token: " + token);
        let newRegister = this.RegisterForm.value
        let user = new User(newRegister);
        user.token_device = token
        this.registerservice.mobile_registergetdata(user).subscribe(
            result => {
                console.log(result)
                if (result === true) {
                    this.router.navigate(['/main']);
                } else {
                    this.areWeWaiting = false;
                }
            });
          },
            (errorMessage: String) => {
                console.log(errorMessage)
                self.updateMessage(JSON.stringify(errorMessage));
            }
        );
    }