简体   繁体   English

单击使用离子和Firebase云消息传递的通知后,打开页面

[英]Open a page after click on notification using ionic and firebase cloud messaging

Good evening. 晚上好。 I am trying to handle notification click in such a way that when the user click on a notification, a specific page of my application is opened. 我试图以这样一种方式来处理通知单击:当用户单击通知时,将打开我的应用程序的特定页面。 I am using FIREBASE COULD MESSAGING AND IONIC 3 我正在使用FIREBASE COULD MESSAGING和IONIC 3

Here is the code of the app.component.ts file in witch the code for handling notification is written : 这是在app.component.ts文件中的代码,用于编写通知的代码:

import { Platform, Nav, ToastController } from 'ionic-angular';

import { HomePage } from '../pages/home/home';

import { Component, ViewChild } from '@angular/core';

import { FCM } from '@ionic-native/fcm';

import { Signup } from '../pages/signup/signup';

   @Component({
      templateUrl: 'app.html',
      selector: 'Myappname',
   })
   export class MyApp {

        @ViewChild(Nav) nv: Nav;

        rootPage: any = HomePage;

        constructor(public fcm: FCM, platform: Platform) {

          platform.ready().then(() => {

                  fcm.onNotification().subscribe(data => {

                         if (data.wasTapped) {

                              this.nv.push(Signup);

                         } else {

                           console.log("Received in foreground");

                         }
                 })
         });
    }
 }         

When the nofication is received on the mobile device, if the user click on it, only the home page is displayed and he is note redirected to the signup page as specified in the code. 当在移动设备上接收到提名时,如果用户单击它,则仅显示主页,并且注释将他重定向到代码中指定的注册页面。

Any helps ? 有帮助吗? Thanks. 谢谢。

Try setting the root page as the signup page, hopefully it will work: 尝试将根页面设置为注册页面,希望它可以工作:

this.nv.setRoot(Signup);

and if that doesnt work try adding this before your fcm.onNotification() 如果这不起作用,请尝试在您的fcm.onNotification()之前添加它

var $this = this;

and then use $this to reference inside your if statement // (wasTapped) 然后在$ if语句中使用$ this引用//(wasTapped)

$this.nv.setRoot(Signup);

I finally found the solution. 我终于找到了解决方案。 as i was using firebase cloud functions to send the notification, here is the code i used to make the onNotification() work when a user click on the notification received. 就像我使用Firebase云函数发送通知一样,这是当用户单击收到的通知时用于使onNotification()工作的代码。

exports.Hello = functions.database.ref('/users/{userId}').onCreate(event=>{

         admin.messaging().sendToTopic('all', {
              data:{
                 "key1": value1,
                 "key2": value2
              },
              notification: {
                  clickAction : "FCM_PLUGIN_ACTIVITY",
                  sound : "default",
                  title : "Notification title",
                  body : "message content"
              }
         });

});

So We must set clickAction property of the notification object to FCM_PLUGIN_ACTIVITY to make onNotification() method execute when the user tapped on the notification. 因此,我们必须将通知对象的clickAction属性设置为FCM_PLUGIN_ACTIVITY ,以便在用户点击通知时执行onNotification()方法。

Here is a code exemple for the app.component.ts in witch the onNotification() method is implemented. 这是实现onNotification()方法的app.component.ts的代码示例

  import { Platform, Nav, ToastController } from 'ionic-angular';

  import { HomePage } from '../pages/home/home';

  import { Component, ViewChild } from '@angular/core';

  import { FCM } from '@ionic-native/fcm';

  import { Signup } from '../pages/signup/signup';

  @Component({
        templateUrl: 'app.html',
        selector: 'Myappname',
  })
  export class MyApp {

    @ViewChild(Nav) nv: Nav;

    rootPage: any = HomePage;

    constructor(public fcm: FCM, platform: Platform) {

      platform.ready().then(() => {

              fcm.onNotification().subscribe(data => {

                     if (data.wasTapped) {

                          this.nv.push(Signup);

                     } else {

                       console.log("Received in foreground");

                     }
             })
           });
      }
  }         

AND now, it works fine ! 现在,它可以正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM