2017-10-11 121 views
0

通知出現,但點擊它們後,只會再次打開應用程序。我想要的是點擊通知後,它會打開一個特定項目。離子2:點擊時發送推送通知

在Laravel中,我使用用於Firebase雲消息傳遞(FCM)的brozot/Laravel-FCM包發送通知,另一方面,我使用Ionic push notifications在通知托盤中接收和顯示通知。

如果我在Laravel上沒有使用setClickAction(),則Ionic應用程序會在單擊通知後打開,但如果我設置了setClickAction(),則不會發生任何情況。通知僅僅消失。

Laravel代碼

$notificationBuilder = new PayloadNotificationBuilder('my title'); 
$notificationBuilder->setBody('Hello world') 
        ->setSound('default') 
        ->setClickAction('window.doSomething'); 
$notification = $notificationBuilder->build(); 

離子2框架樣品

import { Component, ViewChild } from '@angular/core'; 
import { Platform, Nav, MenuController, ModalController, Events, AlertController } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { Push, PushObject, PushOptions } from '@ionic-native/push'; 
import { Storage } from '@ionic/storage'; 

import { 
    SearchPage 
} from '../pages/pages'; 

@Component({ 
    templateUrl: 'app.html' 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 

    rootPage: any = SearchPage; 

    constructor(
     platform: Platform, 
     statusBar: StatusBar, 
     splashScreen: SplashScreen, 
     private menu: MenuController, 
     private modalCtrl: ModalController, 
     private events: Events, 
     private push: Push, 
     private alertCtrl: AlertController, 
     private storage: Storage 
    ) { 

     platform.ready().then(() => { 
      // Okay, so the platform is ready and our plugins are available. 
      // Here you can do any higher level native things you might need. 
      statusBar.styleDefault(); 
      splashScreen.hide(); 
     }); 
     this.pushSetup(); 
    } 

    pushSetup() { 
     const options: PushOptions = { 
      android: { 
       senderID: 'xxxxxxxxxxx', 
       forceShow: true 
      }, 
      ios: { 
       senderID: 'xxxxxxxxxxx', 
       alert: 'true', 
       badge: true, 
       sound: 'true' 
      }, 
      windows: {}, 
      browser: { 
       pushServiceURL: 'http://push.api.phonegap.com/v1/push' 
      } 
     }; 

     const pushObject: PushObject = this.push.init(options); 

     pushObject.on('notification').subscribe((notification: any) => { 

     }); 

     pushObject.on('registration').subscribe((registration: any) => { 
      alert(registration.id); 
     }); 

     pushObject.on('error').subscribe(error => alert('Error with Push plugin' + error)); 

    } 

} 

(<any>window).doSomething = function() { 
    alert('doSomething called'); 
} 

我缺少什麼?

+0

我實施了同樣的Ionic的One-Signal和Firebase功能(都是免費的)。如果你願意改變插件將它作爲答案 – Webruster

+0

是的,我願意 – MTA

+0

請檢查我的解決方案 – Webruster

回答

0

有這些步驟需要爲一般的信號推送通知工作要做,以實現

  1. 創建OneSignal帳戶
  2. 添加新的應用的一個信號,首先配置爲Android (您可以針對任何平臺,但我現在專注於Android)。您需要獲取Google服務器密鑰和Google項目ID。

  3. 可以使用this步驟從火力地堡以上調

  4. 現在,我們正與配置OneSignal賬戶完成的,現在用ionic使用cordova plugin

在Ionic2整合得到:

OneSignal.startInit(//google Server Key, //Google ProjectId); 
      OneSignal.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification); 
      OneSignal.setSubscription(true); 
      OneSignal.handleNotificationReceived().subscribe(() => { 
       // handle received here how you wish. 


       // this.goToReleatedPage(data.Key, data.Value); 
      }); 
      OneSignal.handleNotificationOpened().subscribe((data: any) => { 
       //console.log('MyData'+ JSON.stringify(data.additionalData)); 
       this.parseObject(data); 
      }); 

      OneSignal.endInit(); 

ParsingObject in離子

public parseObject(obj) { 
     for (var key in obj) { 
      this.goToReleatedPage(key, obj[key]); 
      if (obj[key] instanceof Object) { 
       this.parseObject(obj[key]); 
      } 
     } 
    } 

goToReleatedPage方法

public goToReleatedPage(Key, Value) { 
     //console.log("Pagename"+" " + Key + "ID" +" " + Value); 
     if (Key === 'xxxx') { 
      this.navCtrl.push(xxxPage, { 
       id: Value 
      }); 
     } else if (Key === 'Foo') { 
      this.navCtrl.push(foosPage, { 
       id: Value, 

      }); 
     } else if (Key === 'bar') { 
      this.navCtrl.push(barPage, { 
       id: Value 
      }); 
     } 

    } 

雖然從OneSignal發送消息,你需要指定你需要打開它page和要傳遞Id如下

enter image description here