代码之家  ›  专栏  ›  技术社区  ›  Remi Sture

在Ionic/Cordova应用程序中,如何确保在应用程序关闭时触发onNotificationOpen回调?

  •  1
  • Remi Sture  · 技术社区  · 8 年前

    我很难弄清楚为什么iOS应用程序在关闭或终止时无法注册推送通知的点击。它在前台模式下工作得很好,后台模式下只要应用程序没有被杀死。

    当应用程序“死掉”时 onNotificationOpen 回调从未被触发,因此我无法将用户路由到应用程序中所需的位置。

    有人能指出我的错误,或者给我一个有效的例子吗?文件 cordova-plugin-firebase 对于推送通知非常少。

    应用组件

    this.platform.ready().then(() => {
       if (this.platform.is('cordova')) {
          this.pushProvider.init();
    
          this.platform.resume.subscribe(() => {
             this.pushProvider.init();
          });
       }
    });
    

    推送提供商

    import { Injectable } from '@angular/core';
    import { Firebase }   from '@ionic-native/firebase';
    import { Platform }   from 'ionic-angular';
    
    @Injectable()
    export class PushNotificationsProvider {
        constructor (private firebase: Firebase, private platform: Platform) {
        }
    
        init () {
            if (!this.platform.is('cordova')) {
                return;
            }
    
            if (this.platform.is('ios')) {
                this.firebase.grantPermission().then(() => {
                    this.initListeners();
                });
            } else if (this.platform.is('android')) {
                this.firebase.hasPermission().then(data => {
                    if (data && data.isEnabled) {
                        this.initListeners();
                    }
                });
            }
        }
    
        initListeners () {
            this.firebase.onNotificationOpen().subscribe((notification: any) => {
                this.handleNotification(notification);
            });
        }
    
        handleNotification (notification) {
            if (!notification) {
                return;
            }
    
            if (notification.tap) {
                this.backgroundNotification(notification);
            } else {
                this.foregroundNotification(notification);
            }
        }
    
        backgroundNotification (notification) {
            const message = (notification.aps || {}).alert;
            alert('*** FMC: opened in background: ' + message);
        }
    
        foregroundNotification (notification) {
            const message = (notification.aps || {}).alert;
            alert('*** FMC: opened in foreground: ' + message);
        }
    }
    

    包.json

    {
       "ionic-angular": "3.9.2", 
       "cordova-plugin-firebase": "^1.0.2"
       "@ionic-native/firebase": "^4.7.0"
    }
    
    0 回复  |  直到 8 年前