在推送通知Ionic 2 iOS上打开页面

时间:2017-09-13 15:12:47

标签: ios cordova ionic2 cordova-plugin-fcm

最近,我一直在我的应用上添加推送通知功能。我正在使用此link上找到的FCN插件,并使用Ionic 2框架进行开发。通知已经发送,但是当我点击它时,它只会打开应用程序的主页,而不是我需要的内页。

我已使用此代码

declare var FCMPlugin;
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;
  horoscopePage:any = HoroscopeHomePage;
  @ViewChild(Nav) nav: Nav;

   **********************

    FCMPlugin.onNotification(function(data){
      if(data.wasTapped){
        //Notification was received on device tray and tapped by the user.
        if(data.topic =="horoskopi" && data.type=="list"){

        console.log( JSON.stringify(data) );

        this.nav.push(this.horoscopePage);

        }

      }else{

        if(data.topic =="horoskopi" && data.type=="list"){

        console.log( JSON.stringify(data) );

        this.nav.push(this.horoscopePage);
        }
      }
  });
  });

不知何故,这行代码this.nav.push(this.horoscopePage);没有做任何事情

当我使用NavController时它会给我这个错误:

MyApp_Host.html:1 ERROR Error: No provider for NavController!
    at injectionError (core.es5.js:1231)
    at noProviderError (core.es5.js:1269)
    at ReflectiveInjector_._throwOrNull (core.es5.js:2770)
    at ReflectiveInjector_._getByKeyDefault (core.es5.js:2809)
    at ReflectiveInjector_._getByKey (core.es5.js:2741)
    at ReflectiveInjector_.get (core.es5.js:2610)
    at AppModuleInjector.NgModuleInjector.get (core.es5.js:3578)
    at resolveDep (core.es5.js:11039)
    at createClass (core.es5.js:10903)
    at createDirectiveInstance (core.es5.js:10723)

2 个答案:

答案 0 :(得分:1)

您可以将public navCtrl: NavController添加到constructor,然后将代码更改为

this.navCtrl.push(this.horoscopePage);

答案 1 :(得分:0)

在我看了Ionic的官方文档之后,我发现我们无法在NavController中使用app.component.ts,因为您无法注入NavController因为任何组件导航控制器是root component的子级,因此无法注入它们。有关更多信息,请点击here

我无法在PageFCM.onNotifications()内部推送scope函数的原因是它更改了应用程序的FCMPlugin.onNotification( (data)=>{ if(data.wasTapped){ //do something } else { //do something } }); 。解决方案很简单:只需使用箭头功能:

`<table class="table table-hover">
  <thead>
    <tr>
      <th>Contact ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Phone </th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
        <tr *ngFor="let contact of contacts">
            <td>{{ contact.ContactID }}</td>
            <td>{{ contact.FirstName }}</td>
            <td>{{ contact.LastName }}</td>
            <td>{{ contact.Phone }}</td>
            <td>{{ contact.Email }}</td>
            <a [routerLink]="['/contactedit', contact.ContactID]">Edit</a>
            <!-- <td><a (click) = "onEdit( contact.ContactID )" class="btn btn-primary" >Edit</a> </td> -->
            <td><a (click) = "onDelete( contact.ContactID)" class="btn btn-primary" >Delete</a></td>

        </tr>
  </tbody>
</table>`