Notifications

 

Avant-propos

Les push notifications sont des événements prévenant l’utilisateur de l’apparition d’une nouveauté dans une application. Ces push sont généralement créés lors de l’envoi d’une information par le serveur communiquant avec l’application.

 

Exemple de marquage

Pour le marquage des push, il est nécessaire d’avoir une classe IntentService. Cette classe va récupérer l’information du serveur pour la génération du push de l’application. Grâce à cela, des paramètres pourront être ajoutés à la notification.

Exemple de code pour créer une notification contenant des informations :

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());
    }
}

Cet Intent sera ensuite utilisé par la notification.

NB : Pour marquer la suppression d’une notification, il faut également créer un BroadcastReceiver spécifique

Dans le fichier AndroidManifest.xml :

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

Dans le fichier 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();
    }
}

Dans la vue appelée lors du clic sur le push, il est désormais possible de récupérer les informations contenues dans la notification et donc de la marquer.

Exemple :
On récupère l’Intent de la notification et on vérifie que la donnée voulue existe :

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

Une interface est accessible via notre marketplace si vous désirez simplifier le marquage de vos liens sans avoir besoin de connaître la signification de chaque champ. Veuillez noter que l 'interface a été créé par notre communauté et peut être améliorée, n 'hésitez pas à nous envoyer tous vos retours !

Dernière mise à jour : 18/05/2018