首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用离子3和Firebase消息从通知栏打开特定页面?

如何使用离子3和Firebase消息从通知栏打开特定页面?
EN

Stack Overflow用户
提问于 2017-07-18 09:07:48
回答 1查看 870关注 0票数 1

我已经遵循了关于推送通知的Ionic 3文档的说明。

当我试图发送通知和我的应用程序的背景,我不能触发‘通知’事件,所以我不能通过特定的页面。

但是当我的应用程序在前台时,“通知”事件就会自动触发。

我用:

  1. 离子3作为移动框架
  2. 作为消息服务的Firebase Cloud Messagins
  3. Laravel与larave-fcm插件一起发送消息

后端-用于将消息推送到防火墙的Laravel代码

代码语言:javascript
复制
    $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上的离子型应用程序构造器

代码语言:javascript
复制
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()

代码语言:javascript
复制
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));
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-20 01:54:11

我从这里那里找到了答案。

我应该像这样寄给fcm

代码语言:javascript
复制
{
"data" : {
    "title": "Test Notification",
    "body": "This offer expires at 11:30 or whatever",
    "notId": 10,
    "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}

关于laravel刚刚从PayloadDataBuilder.中设置标题、正文、notId On addData函数

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45162240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档