Blog

How to implement NotificationFilter and GeotriggerHandler for Phonegap/Cordova/Ionic

Please note: Support for Phonegap/Cordova/Ionic has been deprecated. See our legacy documentation here.

It is possible to implement a remote notification filter and/or a remote geotrigger handler in your app. When the device enters the geofence area of a notification or geotrigger, the corresponding remote endpoint is called using a *HTTP POST* request. The body contains a list of all notifications/geotriggers triggered in a JSON format.
If you want to segment yours users or personalize the notifications and geotriggers without the requirement of a reliable internet connection you can use a native notification filter and geotrigger handler. Yet, this needs to be performed either in Java for Android or in Objective-C for iOS. This can become a challenge when you’re not experienced with these languages. We expect that at the end of this article you know how to implement your own notification filter and geotrigger handler for iOS and Android regardless of your technical background.

Android

When you include our plugin in your phonegap/cordova/ionic app, it already comes with an implementation for the notification filter and the geotrigger handler. We start from there.

We use this example and continue from there.

Notification Filter for Android

So far the object to override the Notification filter is already there, but Android is not aware of it yet, in order for Android to recognize the RemoteNotificationFilter it needs to be registered in the AndroidManifest.xml. The easiest way of doing that, is by adding a config-file element to the plugin.xml file. Then it will be merged into the AndroidManifest.xml at build time.

<config-file target=”AndroidManifest.xml” parent=”/manifest/application”>

<service android:name=”com.plotprojects.cordova.RemoteGeotriggerHandler” android:exported=”false” />            

</config-file>

Keep in mind that in case you need to add extra files they need to be registered in the plugin.xml file, otherwise the project won’t compile. The source-file section of the Cordova documentation explains it more extensively.

Once that’s done we can change personalize it by changing the implementation of the method filterNotification inside the RemoteNotificationFilter.

*Default Notification filter implementation

On the following example we show you how to replace the filter for the notifications displayed for the users.

*Example Notification filter implementation

In this example, a filter was created to only send notifications if the notification message contains “Amsterdam”.

You can see a more detailed description on how to filter your notifications at Using the Notification Filter to Increase Relevance.

Geotrigger Handler for Android

The same principle for the notification filter is also applied here. The object to override the geotrigger handler is already there, but Android is not aware of it. In order for Android to recognize the RemoteGeotriggerHandler we also need to register it in the AndroidManifest.xml. The easiest way of doing that is by adding a config-file element to the plugin.xml file. Then it will be merged into the AndroidManifest.xml at build time.

<config-file target=”AndroidManifest.xml” parent=”/manifest/application”>

<service android:name=”com.plotprojects.cordova.RemoteGeotriggerHandler” android:exported=”false” />            

</config-file>

Keep in mind that in case you need to add more files they need to be registered in the plugin.xml, otherwise the project won’t compile. The source-file section of the Cordova documentation explains it more extensively.
Once that’s done we can change personalize it by changing the implementation of the method handleGeotriggers inside the RemoteGeotriggerHandler.

*Default Geotrigger handler implementation

On the following example we show you how to replace the filter for the geotrigger handler.

*Example Geotrigger handler implementation

This handler only handles a geotrigger if the data field contains “Amsterdam”.

You can see a more detailed description on how to handle your geotriggers at Using the Geotrigger Handler inside your app.

iOS

On iOS there are two ways of overriding the NotificationFilter, we can do it natively and by javascript. On the other hand, the geotrigger handler can only be overridden natively.

Javascript Notification Filter for iOS

The notifications are intercepted before they are shown by using the filterCallback.

Here’s an example on how to change the notification message and the data before they are shown to the users.

*Example notification filter implementation with Javascript

Native Notification Filter for iOS

To do this in the native way we should go to our plugin, and look for the PlotPlotDelegate.m.

Here you can find the method plotFilterNotifications.

*Default Notification filter implementation

On the following example, we show how to change the message of the notifications displayed for the user.

*Example Notification filter implementation

Further details on how to override the notification filter are described at Using the Notification Filter to Increase Relevance.

Native Geotrigger handler for iOS

The PlotPlotDelegate.m doesn’t have any implementation of the geotrigger handler yet, so we can to add ours.

*Example Geotrigger handler implementation

This example shows how to check the triggered geotriggers before deciding if they should be flagged as handled. Keep in mind that you always have to mark the geotriggers as handled in order for them to be sent.

Further details on how to override the geotrigger handler are described at Using the Geotrigger Handler inside your app.

Spread the love

Get in touch with us!