Post 网络服务中的令牌设备,使用 Nativescript
Post token device in webservice, using Nativescript
请与我分享任何想法,如何在从 onRegisterButtonTap(){}
生成的网络服务和令牌设备中 post。
像下面你可以看到我的代码组件。我想添加这个代码和令牌设备。
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 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;
}
}));
}
我的用户 Class 如下所示:
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();
}}
像下面你可以看到我的代码组件。
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";
}
}
有什么想法吗?
非常感谢。
试试这个:
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));
}
);
}
请与我分享任何想法,如何在从 onRegisterButtonTap(){}
生成的网络服务和令牌设备中 post。
像下面你可以看到我的代码组件。我想添加这个代码和令牌设备。
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 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;
}
}));
}
我的用户 Class 如下所示:
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();
}}
像下面你可以看到我的代码组件。
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";
}
}
有什么想法吗?
非常感谢。
试试这个:
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));
}
);
}