离子2:点击时发送推送通知

问题描述:

通知出现,但点击它们后,只会再次打开应用程序。我想要的是点击通知后,它会打开一个特定项目。离子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

有这些步骤需要为一般的信号推送通知工作要做,以实现

  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