2017-05-29 153 views
1

我正在使用native firebase插件的推送通知功能。推送通知不起作用背景

當前的行爲:

它與foreground.But工作正常時,應用程序在後臺,我要儘量挖掘它顯示undefined。請看到視頻below.I已經測試了這個通知在Anroid設備上。

預期的行爲:

當我點擊通知消息應該打開的應用程序,並正確顯示該消息。

Video

app.component.ts

constructor(public platform: Platform) 
{ 
    platform.ready().then(() => { 
      this.onNotificationOpen(); 
     }); 
} 

onNotificationOpen(): void { 
     if (this.platform.is('cordova')) { 
      this.firebase.onNotificationOpen() 
       .subscribe(res => { 
        if (res.tap) {//background mode 
         console.log(res.body); 
         this.showAlert(res.body); 
        } else if (!res.tap) {//foreground mode 
         console.log(res.body); 
         this.showAlert(res.body); 
        } 
       }); 
     } 
    } 

     showAlert(message: string): void { 
     let alert = this.alertCtrl.create({ 
      title: 'Push Notification', 
      subTitle: message, 
      buttons: ['OK'] 
     }); 
     alert.present(); 
    } 

的package.json

{ 
    "name": "ionic-hello-world", 
    "author": "Ionic Framework", 
    "homepage": "http://ionicframework.com/", 
    "private": true, 
    "scripts": { 
    "clean": "ionic-app-scripts clean", 
    "build": "ionic-app-scripts build", 
    "ionic:build": "ionic-app-scripts build", 
    "ionic:serve": "ionic-app-scripts serve" 
    }, 
    "dependencies": { 
    "@angular/common": "4.1.0", 
    "@angular/compiler": "4.1.0", 
    "@angular/compiler-cli": "4.1.0", 
    "@angular/core": "4.1.0", 
    "@angular/forms": "4.1.0", 
    "@angular/http": "4.1.0", 
    "@angular/platform-browser": "4.1.0", 
    "@angular/platform-browser-dynamic": "4.1.0", 
    "@ionic-native/core": "3.6.1", 
    "@ionic-native/device": "3.6.1", 
    "@ionic-native/facebook": "^3.10.0", 
    "@ionic-native/firebase": "^3.10.3", 
    "@ionic-native/keyboard": "^3.10.3", 
    "@ionic-native/splash-screen": "3.6.1", 
    "@ionic-native/status-bar": "3.6.1", 
    "@ionic/cli-plugin-cordova": "^1.2.0", 
    "@ionic/cli-plugin-ionic-angular": "^1.2.0", 
    "@ionic/storage": "2.0.1", 
    "@ngrx/core": "^1.2.0", 
    "@ngrx/store": "^2.2.1", 
    "d3": "^4.7.4", 
    "firebase": "^3.7.5", 
    "ionic-angular": "3.2.1", 
    "ionicons": "3.0.0", 
    "moment": "^2.18.1", 
    "pouchdb": "^6.1.2", 
    "pouchdb-adapter-cordova-sqlite": "^2.0.2", 
    "rxjs": "5.1.1", 
    "sw-toolbox": "3.6.0", 
    "zone.js": "0.8.10" 
    }, 
    "devDependencies": { 
    "@ionic/app-scripts": "1.3.7", 
    "@ionic/cli-plugin-cordova": "1.2.1", 
    "@ionic/cli-plugin-ionic-angular": "1.2.0", 
    "typescript": "~2.2.1" 
    }, 
    "cordovaPlugins": [ 
    "cordova-plugin-whitelist", 
    "cordova-plugin-console", 
    "cordova-plugin-statusbar", 
    "cordova-plugin-device", 
    "cordova-plugin-splashscreen", 
    "ionic-plugin-keyboard" 
    ], 
    "cordovaPlatforms": [ 
    "ios", 
    { 
     "platform": "ios", 
     "version": "", 
     "locator": "ios" 
    } 
    ], 
    "description": "helloWorld: An Ionic project" 
} 
+1

你從火力控制檯發送推送通知?我沒有使用相同的插件,但請注意,當應用程序在後臺運行時(因爲標題和正文顯示在通知本身中),AFAIK firebase不會在通知中發送相同的數據。請看看[這個答案](https://stackoverflow.com/questions/43928185/how-to-read-firebase-push-notification-content-and-fire-a-method-in-ionic2/43950976# 43950976)(特別是它的最後一部分) – sebaferreras

+0

哦....你太棒了。你可以把最後一部分放在Git repo鏈接嗎?謝謝@sebaferreras – Sampath

+0

Np!我已經按要求添加了這些部分:) – sebaferreras

回答

1

我建議使用cordova-plugin-firebase代替。你可以看看this repo看看如何使用該插件。

請注意,如果應用程序在前臺運行,或者如果應用程序在通知到達時關閉,則發送的通知內容將不相同。爲了搞定,發送通知時,添加title,並在高級選項部分body

enter image description here

+0

有一個問題。可以告訴我,在Git倉庫中使用此方法的任務是什麼?this.saveToken(token);'? – Sampath

+1

只是用於,如果你想發送用戶的令牌到你的服務器 – sebaferreras

+1

確定。謝謝:) – Sampath