微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 IOS 中处于前台状态时的离子推送通知,不断收到重复的通知,直到我从后台终止应用程序

如何解决在 IOS 中处于前台状态时的离子推送通知,不断收到重复的通知,直到我从后台终止应用程序

我正在使用cordova-plugin-local-notification来处理前台推送通知。在android中它工作正常,但在IOS通知即将到来,但不断重复的通知被触发,直到我存在应用程序或从后台删除.

这里是 package.json 文件

{
  "name": "vidut","version": "0.0.1","author": "Ionic Framework","homepage": "https://ionicframework.com/","scripts": {
    "ng": "ng","start": "ng serve","build": "ng build","test": "ng test","lint": "ng lint","e2e": "ng e2e"
  },"private": true,"dependencies": {
    "@angular/common": "~8.2.14","@angular/core": "~8.2.14","@angular/forms": "~8.2.14","@angular/platform-browser": "~8.2.14","@angular/platform-browser-dynamic": "~8.2.14","@angular/router": "~8.2.14","@ionic-native/core": "^5.31.1","@ionic-native/device": "^5.31.1","@ionic-native/fcm": "^5.31.1","@ionic-native/keyboard": "^5.31.1","@ionic-native/local-notifications": "^5.31.1","@ionic-native/preview-any-file": "^5.31.1","@ionic-native/screen-orientation": "^5.31.1","@ionic-native/splash-screen": "^5.31.1","@ionic-native/status-bar": "^5.31.1","@ionic/angular": "^5.5.4","bootstrap": "^4.4.1","cordova-browser": "6.0.0","cordova-ios": "6.2.0","cordova-plugin-screen-orientation": "^3.0.2","core-js": "^2.5.4","es6-promise-plugin": "^4.2.2","highcharts": "^8.1.0","highcharts-angular": "^2.4.0","rxjs": "~6.5.1","tslib": "^1.9.0","zone.js": "~0.9.1"
  },"devDependencies": {
    "@angular-devkit/build-angular": "^0.803.29","@angular/cli": "~8.3.23","@angular/compiler": "~8.2.14","@angular/compiler-cli": "~8.2.14","@angular/language-service": "~8.2.14","@ionic/angular-toolkit": "^2.1.1","@ionic/lab": "3.2.10","@types/jasmine": "~3.3.8","@types/jasminewd2": "~2.0.3","@types/node": "~8.9.4","codelyzer": "^5.0.0","cordova-android": "^9.0.0","cordova-plugin-badge": "^0.8.8","cordova-plugin-device": "^2.0.3","cordova-plugin-fcm-with-dependecy-updated": "^7.8.0","cordova-plugin-ionic-keyboard": "^2.2.0","cordova-plugin-ionic-webview": "^5.0.0","cordova-plugin-local-notification": "^0.9.0-beta.2","cordova-plugin-preview-any-file": "^0.2.3","cordova-plugin-splashscreen": "^6.0.0","cordova-plugin-statusbar": "^2.4.3","cordova-plugin-whitelist": "^1.3.4","jasmine-core": "~3.4.0","jasmine-spec-reporter": "~4.2.1","karma": "~4.1.0","karma-chrome-launcher": "~2.2.0","karma-coverage-istanbul-reporter": "~2.0.1","karma-jasmine": "~2.0.1","karma-jasmine-html-reporter": "^1.4.0","protractor": "~5.4.0","ts-node": "~7.0.0","tslint": "~5.15.0","typescript": "~3.4.3"
  },"description": "An Ionic project","cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},"cordova-plugin-statusbar": {},"cordova-plugin-device": {},"cordova-plugin-splashscreen": {},"cordova-plugin-ionic-keyboard": {},"cordova-plugin-screen-orientation": {},"cordova-plugin-preview-any-file": {},"cordova-plugin-local-notification": {},"cordova-plugin-fcm-with-dependecy-updated": {
        "IOS_FIREBASE_MESSAGING_VERSION": "~> 7.4.0"
      },"cordova-plugin-ionic-webview": {}
    },"platforms": [
      "android","browser","ios"
    ]
  }
}

这是 app.component.ts 文件

// Push notification starts from here
      this.fcm.getToken().then(token => {
        localStorage.setItem('fcm_token',token);
        console.log('@@@ app.component fcm_token' + token);
      });


      this.fcm.getAPNSToken().then(token => {
        console.log('APNSToken:'+token);
      });
 
      this.fcm.onTokenRefresh().subscribe(token => {
        localStorage.setItem('fcm_rtoken',token);
        console.log('@@@ app.component fcm_rtoken' + token);
      });

      this.fcm.onNotification().subscribe(data => {
        console.log('@@@ Push notification data: ' + JSON.stringify(data));
        if (data.wasTapped) {
          console.log('@@@ app.component Received in background');
          if (data.type == 'Individual' && localStorage.getItem('loginValue').toLowerCase() == 'login') {
            this.navController.navigateForward('/notifications');
          } else if (data.type == 'Global' && localStorage.getItem('loginValue').toLowerCase() == 'login') {
            this.navController.navigateForward('/globalnotifications');
          }
        } else {
          console.log('@@@ app.component Received in foreground');
          // this.router.navigate(['/notifications',data.message]);
          if (data.type == 'Individual' && localStorage.getItem('loginValue').toLowerCase() == 'login') {
            //this.navController.navigateForward('/notifications');
            this.localnotifications.schedule({
              title: 'Vidyut Appliance Status',text: data.body,foreground:true
            });
          } else if (data.type == 'Global' && localStorage.getItem('loginValue').toLowerCase() == 'login') {
            //this.navController.navigateForward('/globalnotifications');
            this.localnotifications.schedule({
              title: 'Vidyut Appliance Status',foreground:true
            });
          }
          else{
            this.localnotifications.schedule({
              title: 'Vidyut Appliance Status',foreground:true
            });
          }
        }
      });

      this.fcm.hasPermission().then(hasPermission =>{
        if(hasPermission){
          console.log("has permission!");
        }
        else{
          try{
            this.fcm.requestPushPermission();
            console.log("requesting for permission");
          }
          catch(error){
            console.log("permission denied");
          }
          
          
        }
      });
      this.fcm.clearallNotifications();

任何人都可以对此问题有所了解。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。