如何使用 ionic 3 和 Firebase Cloud Messaging 从通知栏打开特定页面?

How to open specific page from notification bar using ionic 3 and Firebase Cloud Messaging?

我已经按照 Ionic 3 文档中有关推送通知的说明进行操作。

当我尝试在后台发送通知和我的应用程序时,我无法触发 'notification' 事件,因此我无法浏览特定页面。

但是当我的应用程序在前台时,'notification' 事件自动触发。

我使用:

  1. Ionic 3 作为移动框架

  2. Firebase Cloud Messagins 作为消息服务

  3. Laravel 用 larave-fcm 插件发送消息

后端 - Laravel 将消息推送到 firebase 的代码

    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60*20)->setContentAvailable(true);

    $notificationBuilder = new PayloadNotificationBuilder('Hello');
    $notificationBuilder->setBody('Hello world')->setSound('default');

    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData(['custom' => 'test']);

    $option = $optionBuilder->build();
    $notification = $notificationBuilder->build();
    $data = $dataBuilder->build();

    $token = "token";

    $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

    $success = $downstreamResponse->numberSuccess();
    $failure = $downstreamResponse->numberFailure();
    $modification = $downstreamResponse->numberModification();

    echo 'Success: '.$success;
    echo "<br>";
    echo 'Failure: '. $failure;
    echo "<br>";
    echo 'Modification: '.$modification;
    print_r($downstreamResponse->tokensToDelete());
    echo "<br>";
    print_r($downstreamResponse->tokensToModify());
    echo "<br>";
    print_r($downstreamResponse->tokensToRetry());
    echo "<br>";
    print_r($downstreamResponse->tokensWithError());

前端 - 我在 app.component.ts

上的离子应用程序构造函数
constructor(private translate: TranslateService, private platform: Platform, settings: Settings, private config: Config, private statusBar: StatusBar, private splashScreen: SplashScreen, public push: Push, public alertCtrl: AlertController, public storage: Storage, private backgroundMode: BackgroundMode) {
this.initTranslate();
this.platform.ready().then(() => {
  if(!this.backgroundMode.isActive) {
    this.backgroundMode.setDefaults({silent: true});
    this.backgroundMode.enable();
  } else {
    this.backgroundMode.disable();
    this.backgroundMode.setDefaults({silent: true});
    this.backgroundMode.enable();
  }      
  this.pushSetup();
  this.storage.get('test').then((val) => {
    if(val == 'news'){
      this.nav.setRoot(TabsPage);
    }
  });         
});
}

函数 pushSetup()

pushSetup() {
const options: PushOptions = {
  android: {
      senderID: '10524067XXXXX'
  },
  ios: {
      alert: 'true',
      badge: true,
      sound: 'false'
  },
  windows: {}
};

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

pushObject.on('notification').subscribe((notification: any) => {
  if(notification.additionalData.foreground){
    let myAlert = this.alertCtrl.create({
      title: 'Push',
      message: JSON.stringify(notification)
    });
    myAlert.present();
  } else {        
    this.storage.set('test', 'news');
  }
});
pushObject.on('registration').subscribe((registration: any) => {
   console.log(registration);
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}

我从中找到了答案 here.

我应该像这样发送到 fcm

{
"data" : {
    "title": "Test Notification",
    "body": "This offer expires at 11:30 or whatever",
    "notId": 10,
    "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}

在 laravel fcm 上仅设置标题,body,notId 在 addData 函数上来自 PayloadDataBuilder.