Handling click on notification when app is closed/minimized/open

So when the app is open and in the foreground, there is no push notifications showing up, so:

  1. Can I make them show up anyway? In a native way? The event is triggered, though the notification doesn’t show.

  2. If not, is there a plugin to make it look like they do in telegram?

Example:

1 Like

Have you looked at the PhoneGap Local Notification Plugin:

I tried that, but I couldn’t get it working and it also says it doesn’t work with iOS 10. I don’t think it specified what exactly didn’t work though :confused: It just simply didnt trigger a notification when I scheduled one and was subscribed to the event, so I assumed it wasn’t working at all.

Is that what is looking the same as Telegram? I suppose I’ll try again tomorrow.

Alright, so I didn’t realize there was an iOS 10 branch. Installed that, and simply by installing the plugin and not using it anywhere, the notifications now show up while the app is in the foreground.

Next issue at hand: Tapping the notification while the app is open (foreground/background) does nothing, it still works as it should when I close the app completely and tap the notification. Any idea? Btw I’m using @ionic/cloud-angular

import { Push, PushToken } from '@ionic/cloud-angular';

this.push.rx.notification().subscribe(notification => {
    // Only happens when the app is coldstarted
    alert(JSON.stringify(notification));
});

Update

Right… So subbing to the notification is only for when the notification is RECEIVED, and there seems to be no way to detect a press, unless you detect that the app is open when a notification was received, and show your own custom in-app notification…

1 Like

This isn’t making any damn sense… How in the world can I

  • Handle a TAP on a notification while the app is CLOSED
    I have this somewhat handled. Only issue is that when there’s multiple notifications, the ones you didn’t tap will trigger the notification event after some random amount of time…
  • Handle a TAP on a notification while the app is MINIMIZED
  • Handle a TAP on a notification while the app is OPEN

? :confounded:

SOLUTION

In case anyone will ever stumble upon this, here’s what I ended up with. Just a reminder that I am using @ionic/cloud-angular

Handle a TAP on a notification while the app is CLOSED

I have this inside my app component ngOnInit()

this.push.rx.notification().subscribe(notification => {
	if(notification.raw.additionalData.coldstart === true && this.coldstartRedirect === false) {
		if(notification.payload && notification.payload["news_article_id"]) {
			this.coldstartRedirect = true;
			// Redirect to article
		}
	} else if(this.platform.is("android")) {
		// For some reason this event is triggered from a CLICK
		// on Android, which only makes it easier to do.. Handle
		// the click here.
	}
});

Why the this.coldstartRedirect?
Because I had issues when there were multiple notifications. The one you tapped would trigger this event first, and the other ones would get triggered waaayyyy after, even if not clicked, causing multiple redirects.

Handle a TAP on a notification while the app is MINIMIZED/OPEN

These go under the same thing. Again, in the app component ngOnInit(), inside platform.ready() I register for push notifications. In the fulfilled promise I setup LocalNotifications to listen for clicks. I put in here all because of timing, if I put it outside (e.g. after the subscription to the coldstart notifications), this would also be triggered for clicks on notifications on a coldstart.

this.push.register().then((t: PushToken) => {
	return this.push.saveToken(t);
}).then(
	(t: PushToken) => {
		LocalNotifications.on("click", (notification, state) => {
			/* 
				Handle click, do same as for coldstarts

				Note that the notification object has different keys
				than the notification object in this push.rx.notification
				subscription.

				Since Android apparently works on clicks at the
				push.rx.notification subscription, this handling
				will only be for iOS
			*/
		});
	},
	err => {
		console.error(err);
	}
);

#iOS 10 issues
You need the iOS 10 branch of the LocalNotifications plugin for this to work on iOS 10, I added the following to my config.xml

<plugin name="https://github.com/katzer/cordova-plugin-local-notifications.git#ios10"/>

You might need to delete the plugins folder and remove->add platforms, at least for me it was slightly buggy.

Updated the title from “Show Push notification in the app like Telegram” to “Handling click on notification when app is closed/minimized/open”