I need to know how can i use notification permission in ionic app on toggle button. I want when user turn off the toggle so user cant get FCM Push notification if toggle is on then the user can get notification. I try to use local storage when user turn off toggle i set the toggle false in localstoreage so when the user open app again the toggle button is off .
<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);
}
);
}
}
from Ionic 3 Set notification permission on toggle button
No comments:
Post a Comment