Notifications

 

Foreword

Push notifications notify the user of new events in an application. Push notifications are generally created when information is sent by the server in communication with the application.

 

Tagging example

To tag push notifications, it’s necessary to have an IntentService class. This class will retrieve information from the server for generation of the application’s push notifications. Thanks to this, parameters can be added to the notification.

Example of code for creating a notification containing information:

package com.atinternet.atinternetdemo;

import android.app.IntentService;
import android.content.Intent;

public class DemoService extends IntentService {

    public DemoService(String name) {
        super(name);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setAutoCancel(true)
                        .setContentTitle("MyNotification")
                        .setSmallIcon(android.R.drawable.ic_dialog_alert)
                        .setContentText("Notification received");

        Intent notificationIntent = new Intent(getApplicationContext(), DestinationActivity.class);
        notificationIntent.putExtra("xto", "AD-10");
        PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
        notificationBuilder.setContentIntent(intent);

        // Catch Delete Notification event
        Intent deleteIntent = new Intent(this, DismissNotificationReceiver.class);
        PendingIntent pendingDeleteIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, deleteIntent, 0);
        notificationBuilder.setDeleteIntent(pendingDeleteIntent);

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(0, notificationBuilder.build());
    }
}

This Intent will then be used by the notification.

NB: In order to tag the deletion of a notification, you have to create a specific BroadcastReceiver

In AndroidManifest.xml:

<receiver
    android:name=".DismissNotificationReceiver"
    android:exported="true">
</receiver>

In DismissNotificationReceiver.java:

package com.atinternet;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.atinternet.tracker.Tracker;

import java.util.HashMap;

public class DismissNotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Track Dismiss notification
        Tracker t = new Tracker(context, new HashMap<String, Object>() {
            {
                put("log", "YOURLOG");
                put("logSSL", "YOURSSLLOG");
                put("site", "YOURSITEID");
            }
        });
        t.Screens().add("DismissNotification").sendView();
    }
}

In the view called during the click on the push notification, it is now possible to retrieve information contained in the notification and therefore tag it.

Example:
We retrieve the notification’s Intent and we verify that the desired data exists:

Tracker tracker = ATInternet.getInstance().getDefaultTracker();
if (getIntent().getStringExtra("xto") != null) {
            tracker.Campaigns().add(getIntent().getStringExtra("xto"));
            tracker.Screens().add("PushNotification").sendView();
}

If you wish to simplify the tagging of your links without having to know the significance of each field, try the interface available in our marketplace. Please note that this interface has been created by our community and can be improved, so feel free to send us your feedback!

Last update: 18/05/2018