Skip to content

Customize Notifications

Styling options

You can set your own icon for the location-based notifications and the push notifications with the methods:

NearItManager.getInstance().setProximityNotificationIcon(R.drawable.ic_my_location_notification);
NearItManager.getInstance().setPushNotificationIcon(R.drawable.ic_my_push_notification);
NearItManager.getInstance().setProximityNotificationIcon(R.drawable.ic_my_location_notification)
NearItManager.getInstance().setPushNotificationIcon(R.drawable.ic_my_push_notification)

Custom Service

To gain complete control over the notification look and behavior you can override out JobIntentService by subclassing the built-in one. Create a subclass of NearBackgroundJobIntentService and override the onHandleWork method:

@Override
protected void onHandleWork(@NonNull Intent intent) {
    // your custom logic. See below.
}
override fun onHandleWork(intent: Intent) {
    // your custom logic. See below.
}

Inside the method do whatever you want with the intent, but be aware that, depending on the target, this might be executed in a jobservice and background limitations apply.

To notify it to the user with the NearIT system notification, call super.sendSimpleNotification(this, intent). This method also sends the proper tracking information to our servers.

To get the content out of the intent and implement you custom logic, you can use the method NearUtils.parseContents(intent, contentListener) and get casted content in the listener callback methods.

IMPORTANT

If you are overriding the default notification mechanism, remember to track the recipe as notified:

TrackingInfo trackingInfo = intent.getParcelableExtra(NearItIntentConstants.TRACKING_INFO);
NearItManager.getInstance().sendTracking(trackingInfo, Recipe.NOTIFIED_STATUS);
val trackingInfo: TrackingInfo = intent.getParcelableExtra(NearItIntentConstants.TRACKING_INFO)
NearItManager.getInstance().sendTracking(trackingInfo, Recipe.NOTIFIED_STATUS)

Then add your custom JobIntentService to the manifest

<service android:name=".MyCustomJobIntentService"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE">
    <intent-filter>
        <action android:name="it.near.sdk.permission.PUSH_MESSAGE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="it.near.sdk.permission.GEO_MESSAGE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

By combination of intent filters, you can customize only one kind of background notifications, or use 2 different intent services for the 2 situations.

Custom ranging notifications

Beacon interaction (beacon ranging) is a peculiar trigger that only works when your app is in the foreground.
NearIT Android SDK will automatically show heads-up notifications.

If you need to disable the default behaviour, call this method in the onCreate method of your application:

{
    @Override
    public void onCreate() {
        super.onCreate();
        NearItManager.getInstance().disableDefaultRangingNotifications();
        // ...
    }
}
{
    override fun onCreate() {
        super.onCreate()
        NearItManager.getInstance().disableDefaultRangingNotifications()
    }
}

And if you want to receive ranging contents and handle them manually, set a proximity listener with the method:

{
    //  ...

    NearItManager.getInstance().addProximityListener(this);
    // remember to remove the listener when the object is being destroyed with 
    // NearItManager.getInstance().removeProximityListener(this);
    //  ...
}

@Override
public void foregroundEvent(Parcelable content, TrackingInfo trackingInfo) {
    // handle the event
    // To extract the content and to have it automatically casted to the appropriate object type
    NearUtils.parseContents(content, trackingInfo, contentsListener);
}
{
    //  ...

    NearItManager.getInstance().addProximityListener(this)
    // remember to remove the listener when the object is being destroyed with 
    // NearItManager.getInstance().removeProximityListener(this)
    //  ...
}

override fun foregroundEvent(content: Parcelable, trackingInfo: TrackingInfo) {
    // handle the event
    // To extract the content and to have it automatically casted to the appropriate object type
    NearUtils.parseContents(content, trackingInfo, contentsListener)
}

Warning: In this situation you will need to write the code for Trackings and to eventually show an In-app notification.