Ionic 3 OneSignal推送通知单击打开页面

时间:2018-02-06 07:19:34

标签: ionic-framework push-notification ionic3 onesignal

我对Ionic 3使用OneSignal推送通知。我想点击通知应用程序打开页面。

  

我的app.component.ts

import { Component, ViewChild } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { OneSignal } from '@ionic-native/onesignal';


import { DuyurularPage } from '../pages/duyurular/duyurular';

@Component({
 templateUrl: 'app.html',
 providers:[OneSignal],
 template: '<ion-nav #myNav [root]="rootPage"></ion-nav>'

})
export class MyApp {
 rootPage:any = 'MenuPage';
 bgColor: string = '#fff';
 @ViewChild('myNav') nav: NavController;

constructor(platform: Platform, statusBar: StatusBar, splashScreen: 
SplashScreen, private oneSignal: OneSignal,) {
  platform.ready().then(() => {

   statusBar.styleDefault();
   splashScreen.hide();

   window["plugins"].OneSignal
  .startInit("**************", "***********")
  .handleNotificationOpened()
  .handleNotificationReceived()
  .endInit();
  });

  this.oneSignal.handleNotificationOpened().subscribe((jsonData) => {
   alert(JSON.stringify(jsonData));
   this.nav.push(DuyurularPage);
  });
  this.oneSignal.handleNotificationReceived().subscribe((jsonData) => 
  {
   alert(JSON.stringify(jsonData));
   this.nav.push(DuyurularPage);
  });
 }

}

当我误会的时候我不会&#39;我知道。通知即将到来但不显示警报或不推DuyurularPage,只需打开主页。

3 个答案:

答案 0 :(得分:1)

如果您需要在通知点击上打开特定页面。遵循技术。 在App.component.ts

          this.oneSignal.handleNotificationOpened().subscribe((data) => {
            // do something when a notification is opened
            console.log('Tapped',data);
             if(data.notification.payload.additionalData.landing_page != undefined && data.notification.payload.additionalData.landing_page != ''){
               this.PushProvider.landing_page = data.notification.payload.additionalData.landing_page;
             }
             if(data.notification.payload.additionalData.product_id != undefined && data.notification.payload.additionalData.product_id != ''){
               this.PushProvider.product_id = data.notification.payload.additionalData.product_id;
             } 
          });
          this.oneSignal.endInit(); 

//在主页上

  if(this.PushProvider.landing_page != undefined){
      console.log('on home if',this.PushProvider.landing_page);
      this.navCtrl.push('offerzonePage',{ from_tab: this.PushProvider.landing_page});
    }
    if(this.PushProvider.product_id != undefined){
      console.log('on home if',this.PushProvider.product_id);
      this.navCtrl.push('ProductPage',{ product_id: this.PushProvider.product_id });
    }

On Pushprovicer.ts

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class PushProvider {


   // Produc ID
   product_id: any;

   // Landing pages
   landing_page: any;

}

答案 1 :(得分:0)

您可以使用以下语法并尝试;

SINT16 tempVal = pDate->month + upDown;
    if (tempVal < 0) {
        tempVal += (12+1);
    } 
    pDate->month % (12 + 1);

您似乎无法在import { Component, ViewChild } from '@angular/core'; import { Platform, NavController } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { OneSignal } from '@ionic-native/onesignal'; import { DuyurularPage } from '../pages/duyurular/duyurular'; @Component({ templateUrl: 'app.html', providers:[OneSignal], template: '<ion-nav #myNav [root]="rootPage"></ion-nav>' }) export class MyApp { rootPage:any = 'MenuPage'; bgColor: string = '#fff'; @ViewChild('myNav') nav: NavController; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private oneSignal: OneSignal,) { platform.ready().then(() => { statusBar.styleDefault(); splashScreen.hide(); this.oneSignal.startInit('*****', '***'); this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert); this.oneSignal.handleNotificationOpened().subscribe((jsonData) => { alert(JSON.stringify(jsonData)); this.nav.push(DuyurularPage); }); this.oneSignal.handleNotificationReceived().subscribe((jsonData) => { alert(JSON.stringify(jsonData)); }); } 中使用NavController

您可以使用NavApp

吗?

答案 2 :(得分:0)

我要做的是创建一个obsevable并在我的应用中需要的任何地方订阅它。每当触发插件回调时,observable都会发出next值。

  • RxJS Observable
  • 在回调中发出下一个值
  • 在您的应用中的任意位置订阅
  • 使用提供的NavCtrl将组件推送到视图中

如果你注入离子Event,那么同样的原则也会有效。

Btw你的构造函数末尾有一个随机逗号

相关问题