【发布时间】:2017-04-22 09:30:28
【问题描述】:
我正在使用带有来自https://github.com/fechanique/cordova-plugin-fcm 的cordova-plugin-fcm 插件的ionic 2
我在将令牌值分配给 deviceId 时遇到问题,console.log 在调用中正确显示该值,但在外部返回 null
代码如下:
declare var FCMPlugin;
@Component({
templateUrl: `app.template.html`
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
public deviceId: any;
appPages: PageObj[] = [
{ title: 'Home', component: HomePage, icon: 'home' },
{ title: 'Profile', component: UserProfilePage, icon: 'calendar' },
{ title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' },
];
rootPage: any;
TutorialPage: TutorialPage;
constructor(platform: Platform,
public menu: MenuController,
private actionSheetCtrl: ActionSheetController,
public authService: AuthService,
public dataService: DataService) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
FCMPlugin.getToken(
function (token) {
console.log('device token is ' + token);
var deviceId = token;
console.log('value of id is' + deviceId);
},
function (err) {
console.log('error retrieving token: ' + err);
}
);
});
}
ngOnInit() {
var self = this;
this.authService.onAuthStateChanged(function (user) {
if (user === null) {
self.nav.setRoot(TutorialPage);
}
});
}
ngAfterViewInit() {
var self = this;
this.authService.onAuthStateChanged(function (user) {
if (user === null) {
self.menu.close();
self.nav.setRoot(TutorialPage);
} else {
console.log("device token upon login is" + this.deviceId);
let uid = self.authService.getLoggedInUser().uid;
console.log("uid at login is " + uid);
self.dataService.setDeviceToken(uid, this.deviceId);
}
});
}
deviceId 的值在 ngAfterViewInit 中显示为 null
终于开始工作了:----
有效的编辑代码是:
declare var FCMPlugin;
@Component({
templateUrl: `app.template.html`
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
public deviceId: any;
appPages: PageObj[] = [
{ title: 'Home', component: HomePage, icon: 'home' },
{ title: 'Profile', component: UserProfilePage, icon: 'calendar' },
{ title: 'Give Feedback', component: FeedbackPage, icon: 'calendar' },
];
rootPage: any;
TutorialPage: TutorialPage;
constructor(platform: Platform,
public menu: MenuController,
private actionSheetCtrl: ActionSheetController,
public authService: AuthService,
public dataService: DataService) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
FCMPlugin.getToken(
token => {
var self = this;
console.log('device token is ' + token);
let devicetoken = token;
console.log('value of id is' + devicetoken);
self.deviceId = devicetoken;
},
function (err) {
console.log('error retrieving token: ' + err);
}
);
});
}
ngOnInit() {
var self = this;
this.authService.onAuthStateChanged(function (user) {
if (user === null) {
self.nav.setRoot(TutorialPage);
}
});
}
ngAfterViewInit() {
var self = this;
this.authService.onAuthStateChanged(function (user) {
if (user === null) {
self.menu.close();
self.nav.setRoot(TutorialPage);
} else {
console.log("device token upon login is" + self.deviceId);
let uid = self.authService.getLoggedInUser().uid;
console.log("uid at login is " + uid);
self.dataService.setDeviceToken(uid, self.deviceId);
}
});
}
【问题讨论】:
标签: javascript cordova push-notification ionic2 cordova-plugins