1

I struggle to figure out why my iOS app can't register clicks on push notifications when the app is closed or terminated. It works just fine in foreground mode, and background mode as long as the app is not killed.

When the app "dead" the onNotificationOpen callback is never fired, so i'm not able to route the user to the desired location in the app.

Can someone point out my mistakes, or provide me with a working example? The documentation for cordova-plugin-firebase is very minimal regarding push notifications.

app.component.ts

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

      this.platform.resume.subscribe(() => {
         this.pushProvider.init();
      });
   }
});

push.provider.ts

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);
    }
}

package.json

{
   "ionic-angular": "3.9.2", 
   "cordova-plugin-firebase": "^1.0.2"
   "@ionic-native/firebase": "^4.7.0"
}
Remi Sture
  • 12,000
  • 5
  • 22
  • 35
  • When your app is terminated, so is the callback that existed. Clicking on the notification opens the app, but then it has to reach the `initListeners()` code before it knows it needs to handle the notification which is after the event has been fired. – BShaps May 22 '18 at 20:05
  • Yes, so what’s the approach to solve this? Seems like non of the apps I use daily have this issue, e.g. Facebook, Strava, Instagram, etc. – Remi Sture May 22 '18 at 20:56
  • I'm not sure what the exact solution would be, but I believe that native apps have access to some data that tells the app whether or not it was opened from a notification. You could build your own cordova plugin that incorporates that behavior if one doesn't already exist, other than that I'm not sure how else you could solve this. This might get you started on how it works on iOS [Handling Push Notifications when app not running iOS](https://stackoverflow.com/questions/38512456/handling-push-notifications-when-app-is-not-running-i-e-totally-killed) – BShaps May 22 '18 at 21:19

0 Answers0