cordova-plugin-firebase: BUG (Android): onNotificationOpen doesn't fire when app is open and in foreground.

UPDATE: This appears to be a bug in Android only, I finally tested things on my iOS devices and everything works as expected. When iOS app is open and a push notification message arrives, the onNotificationOpen fires and I am able to process the payload and create a custom message/popup to the user. This simply does not work in Android - onNotificationOpen never fires.

In the documentation for function onNotificationOpen, the author states:

App is in foreground: User receives the notification data in the JavaScript callback without any notification on the device itself (this is the normal behaviour of push notifications, it is up to you, the developer, to notify the user)

But best I can tell, when the app is opened and a push is received, onNotificationOpen never fires, so the callback never fires.

  window.FirebasePlugin.onNotificationOpen(function(payload) {
    console.log(payload) ;
  }, function(error) {
      console.error(error);
  }) ;
  1. If app is closed, notification arrives, user clicks on notification, app opens and fires onNotificationOpen - if a payload exists, it gets processed as payload.ObjectKey - WORKS
  2. If app is open but in the background, the same behavior as #1. - WORKS
  3. But if app is open and in the foreground, the notification never shows in the status bar (apparently native behavior), but what function fires? It is not onNotificationOpen - DOES NOT WORK. console.log never fires (not even the error one), thus onNotificationOpen is not firing - this is contrary to author’s documentation.

So either its a bug OR the author somehow is referring to another JS callback in his documentation.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 32

Most upvoted comments

initializeFirebase() {
	if(!document.URL.startsWith('http')) {
		// initialize push notifications
		this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
	} else {
		console.log('Push notifications are not enabled since this is not a real device');
	}
}

private initializeFirebaseAndroid(): void {
	this.firebase.getToken()
		.catch((e) => {
			console.error(e);
		})
		.then(token => {
			console.log(`This Android device's token is ${token}`);
		});

		this.firebase.onTokenRefresh().subscribe((token: string) => {
			// save device token
			this.subscribeToPushNotifications();
		}, e => {
			console.error(e);
		});
}

private initializeFirebaseIOS(): Promise<any> {
	return this.firebase.grantPermission()
		.catch((e) => {
			console.error(e);
		})
		.then(() => {
			this.firebase.getToken()
				.catch((e) => {
					console.error(e);
				})
				.then(token => {
					console.log(`This iOS device's token is ${token}`);
				});

			this.firebase.onTokenRefresh().subscribe((token: string) => {
					// save device token
					this.subscribeToPushNotifications();
				}, e => {
					console.error(e);
				});
		});
}
	
private subscribeToPushNotifications() {
	// handle incoming push notifications
	this.firebase.onNotificationOpen().subscribe((pushNotification: Notification) => {
		if (pushNotification.tap) {
			// background received
		} else {
			// foreground received
		}
	}, e => {
		console.error(e).
	});
}

Hello, I’m facing with the same issue . onNotificationOpen is not fire when the app is open/foreground on Android. Is there any available solution ?

Hello @rolinger 😃 I have some issue with onNotificationOpen but the diferent is iam sending the notification from firebase console with a parameters, I use that parameter to navégate in the app when the notification is open for some reason when the app is open and I send a notification the onNotificationOpen fires Well, when the app is close th notification arrived when I tab to open the notification the onNotificationOpen is not fire.