Ionic 3 在切换按钮上设置通知权限
Ionic 3 Set notification permission on toggle button
我需要知道如何在切换按钮上使用 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);
}
);
}
}
正确的做法是编写后端函数来注册注销通知
Eg : /myapi/stopPushnotification
这个 remove/add 来自我们数据库的令牌或跟踪它到另一个 table。在应用程序上,我们有开关可以打开和关闭它。这是最好的方法。
1) 创建服务
notification service.ts
====================
appConfig = {
pushNotificationStatus : true // false
}
2) 在切换按钮上调用 enable/disable pushNotificationStatus 并将 notification service.ts
appconfig 中的值更新为:-
// suppose putservice call for enable/disable push notification
constructor(public notifServ: NotificationService){}
ontoggle(){
this.https.post(url,{}).subscribe(res =>{
this.notifServ.appConfig.pushNotificationStatus = true // false based on service call
})
}
3) app.component.ts or component load
, 检查 pushNotification 是启用还是禁用
对于某些呼叫中的应用程序(可能是配置文件呼叫或发送 pushNotification 状态的任何呼叫
应用程序的用户)
a) 在
中调用并更新值
this.notifServ.appConfig.pushNotificationStatus = true // false
或者,
在 localStorage 中执行此操作不是最好的方法,但如果您想在客户端级别处理它,则可以这样做:-
For this, whenever your component loads `clear localStorage` or set `push notiifcation key` in localStorage as empty or null or may delete.
注:以上第一种方法即service also works at client level
、just update the appConfig in service and use it as refrence when do enable/disable push notifications.
它很简单,在您的 notify() 函数中,当应用程序打开时,如果切换值为真,则您的用户切换值是假或真 this.fcm.subscribeToTopic('all');
。如果它是假的,那么就取消订阅吧。希望这是你的完美答案
initializeApp() {
this.platform.ready().then(() => {
this.check();
//Notifications
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");
};
})
//end notifications.
if (this.isToggled == true) {
this.fcm.subscribeToTopic('all');
}
else {
this.fcm.unsubscribeFromTopic('all');
}
// 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)
);
if (this.isToggled == true) {
this.fcm.unsubscribeFromTopic('all');
}
else {
this.fcm.unsubscribeFromTopic('all');
}
}
check() {
this.nativeStorage.getItem('toggle')
.then(
(data) => {
console.log(data.property),
this.isToggled = data.property;
console.log(this.isToggled);
}
);
}
我需要知道如何在切换按钮上使用 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);
}
);
}
}
正确的做法是编写后端函数来注册注销通知
Eg : /myapi/stopPushnotification
这个 remove/add 来自我们数据库的令牌或跟踪它到另一个 table。在应用程序上,我们有开关可以打开和关闭它。这是最好的方法。
1) 创建服务
notification service.ts
====================
appConfig = {
pushNotificationStatus : true // false
}
2) 在切换按钮上调用 enable/disable pushNotificationStatus 并将 notification service.ts
appconfig 中的值更新为:-
// suppose putservice call for enable/disable push notification
constructor(public notifServ: NotificationService){}
ontoggle(){
this.https.post(url,{}).subscribe(res =>{
this.notifServ.appConfig.pushNotificationStatus = true // false based on service call
})
}
3) app.component.ts or component load
, 检查 pushNotification 是启用还是禁用
对于某些呼叫中的应用程序(可能是配置文件呼叫或发送 pushNotification 状态的任何呼叫
应用程序的用户)
a) 在
this.notifServ.appConfig.pushNotificationStatus = true // false
或者,
在 localStorage 中执行此操作不是最好的方法,但如果您想在客户端级别处理它,则可以这样做:-
For this, whenever your component loads `clear localStorage` or set `push notiifcation key` in localStorage as empty or null or may delete.
注:以上第一种方法即service also works at client level
、just update the appConfig in service and use it as refrence when do enable/disable push notifications.
它很简单,在您的 notify() 函数中,当应用程序打开时,如果切换值为真,则您的用户切换值是假或真 this.fcm.subscribeToTopic('all');
。如果它是假的,那么就取消订阅吧。希望这是你的完美答案
initializeApp() {
this.platform.ready().then(() => {
this.check();
//Notifications
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");
};
})
//end notifications.
if (this.isToggled == true) {
this.fcm.subscribeToTopic('all');
}
else {
this.fcm.unsubscribeFromTopic('all');
}
// 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)
);
if (this.isToggled == true) {
this.fcm.unsubscribeFromTopic('all');
}
else {
this.fcm.unsubscribeFromTopic('all');
}
}
check() {
this.nativeStorage.getItem('toggle')
.then(
(data) => {
console.log(data.property),
this.isToggled = data.property;
console.log(this.isToggled);
}
);
}