【发布时间】:2019-07-14 20:59:31
【问题描述】:
我需要知道如何在 ionic 应用程序中的切换按钮上使用通知权限。我希望当用户关闭切换时,如果切换打开,用户将无法获得 FCM 推送通知,然后用户可以获得通知。当用户关闭切换时,我尝试使用本地存储,我在 localstoreage 中将切换设置为 false,因此当用户再次打开应用程序时,切换按钮关闭。
<ion-item>
<ion-toggle [(ngModel)]="isToggled" (ionChange)="notify()" item-start checked="true" ></ion-toggle>
<ion-label item-end style="text-align: right;">تلقي الاشعارات
</ion-label>
</ion-item>
.ts
constructor(private nativeStorage: NativeStorage, private push: Push, public platform: Platform, private fcm: FCM, public statusBar: StatusBar, public splashScreen: SplashScreen) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.check();
//Notifications
if(this.isToggled == true){
this.fcm.subscribeToTopic('all');
this.fcm.getToken().then(token=>{
console.log(token);
})
this.fcm.onNotification().subscribe(data=>{
if(data.wasTapped){
this.nav.setRoot(ArticledetailsPage, {x:data.newsid});
console.log("Received in background");
} else {
console.log("Received in foreground");
};
})
if(this.isToggled == true){
this.fcm.subscribeToTopic('marketing');
}
else{
this.fcm.unsubscribeFromTopic('marketing');
}
//end notifications.
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.splashScreen.hide();
});
}
notification(){
this.nav.push(NotificationPage);
}
public notify() {
console.log("Toggled: "+ this.isToggled);
this.nativeStorage.setItem('toggle', {property: this.isToggled, anotherProperty: 'anotherValue'})
.then(
() => console.log('Stored item!'),
error => console.error('Error storing item', error)
);
}
check(){
this.nativeStorage.getItem('toggle')
.then(
(data) => {
console.log(data.property),
this.isToggled = data.property;
console.log(this.isToggled);
}
);
}
}
【问题讨论】:
标签: angular ionic-framework ionic3