Ionic 2:点击

时间:2017-10-11 10:39:19

标签: ionic-framework notifications ionic2

会显示通知,但点击后会再次打开该应用程序。我想要的是点击通知,它打开一个特定的项目。

在Laravel中,我使用brozot/Laravel-FCM Firebase Cloud Messaging(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'); 
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

要实施一般的单信号推送通知,需要执行以下步骤

  1. 创建OneSignal帐户
  2. 在One Signal中添加新APP,首先为Android配置(您可以定位任何平台,但我现在专注于Android)。您需要获取Google服务器密钥和Google Project Id

  3. 您可以使用this步骤

  4. 从Firebase获取以上密钥
  5. 现在我们已完成配置OneSignal帐户,现在使用ionic
  6. 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();
    

    Ionic中的ParsingObject

    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