<commit_before>package ca.etsmtl.applets.etsmobile.service;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Map;

import androidx.core.app.NotificationCompat;
import ca.etsmtl.applets.etsmobile.ApplicationManager;
import ca.etsmtl.applets.etsmobile.model.MonETSNotification;
import ca.etsmtl.applets.etsmobile.ui.activity.NotificationActivity;
import ca.etsmtl.applets.etsmobile.util.Constants;
import ca.etsmtl.applets.etsmobile.util.SecurePreferences;
import ca.etsmtl.applets.etsmobile2.R;

/**
 * Created by gnut3ll4 on 16/10/15.
 */
public class ETSFcmListenerService extends FirebaseMessagingService {

    private static final String TAG = "MyGcmListenerService";
    private static final int NUMBER_OF_NOTIF_TO_DISPLAY = 5;

    /**
     * Called when message is received.
     *
     * @param message containing the data from the sender and from the message itself
     *                (Bundle containing message data as key/value pairs.
     */
    @Override
    public void onMessageReceived(RemoteMessage message) {

        Map<String, String> data = message.getData();

        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(data);
    }

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param data GCM message received.
     */
    private void sendNotification(Map<String, String> data) {
        SecurePreferences securePreferences = new SecurePreferences(this);
        Gson gson = new Gson();

        String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");

        ArrayList<MonETSNotification> receivedNotif = gson.fromJson(
                receivedNotifString,
                new TypeToken<ArrayList<MonETSNotification>>() {
                }.getType());

        if (receivedNotif == null) {
            receivedNotif = new ArrayList<>();
        }

        MonETSNotification nouvelleNotification = getMonETSNotificationFromMap(data);

        receivedNotif.add(nouvelleNotification);

        int numberOfNotifications = receivedNotif.size();
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);<commit_msg>Register a device token to AWS SNS when a new one is generated<commit_after>package ca.etsmtl.applets.etsmobile.service;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import androidx.core.app.NotificationCompat;
import ca.etsmtl.applets.etsmobile.ApplicationManager;
import ca.etsmtl.applets.etsmobile.model.MonETSNotification;
import ca.etsmtl.applets.etsmobile.ui.activity.NotificationActivity;
import ca.etsmtl.applets.etsmobile.util.Constants;
import ca.etsmtl.applets.etsmobile.util.SecurePreferences;
import ca.etsmtl.applets.etsmobile2.R;

/**
 * Created by gnut3ll4 on 16/10/15.
 */
public class ETSFcmListenerService extends FirebaseMessagingService {

    private static final String TAG = "MyGcmListenerService";
    private static final int NUMBER_OF_NOTIF_TO_DISPLAY = 5;

    /**
     * Called when message is received.
     *
     * @param message containing the data from the sender and from the message itself
     *                (Bundle containing message data as key/value pairs.
     */
    @Override
    public void onMessageReceived(RemoteMessage message) {

        Map<String, String> data = message.getData();

        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(data);
    }

    @Override
    public void onNewToken(String token) {
        sendRegistrationToServer(token);
    }

    /**
     * Persist registration to third-party servers.
     * <p/>
     * Modify this method to associate the user's FCM registration token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        ExecutorService executor = Executors.newFixedThreadPool(1);
        CreateEndpointJob worker = new CreateEndpointJob(getApplicationContext());

        worker.setThreadProperties(token,
                ApplicationManager.domaine+"\\"+ApplicationManager.userCredentials.getUsername(),
                getString(R.string.aws_application_arn));
        worker.run();
        executor.execute(worker);
    }

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param data FCM message received.
     */
    private void sendNotification(Map<String, String> data) {
        SecurePreferences securePreferences = new SecurePreferences(this);
        Gson gson = new Gson();

        String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");

        ArrayList<MonETSNotification> receivedNotif = gson.fromJson(
                receivedNotifString,
                new TypeToken<ArrayList<MonETSNotification>>() {
                }.getType());

        if (receivedNotif == null) {
            receivedNotif = new ArrayList<>();
        }

        MonETSNotification nouvelleNotification = getMonETSNotificationFromMap(data);

        receivedNotif.add(nouvelleNotification);

        int numberOfNotifications = receivedNotif.size();
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
{'fraction_non_alphanumeric': 0.08530094172239662, 'fraction_numerical': 0.0034120376688958646, 'mean_word_length': 3.168373151308305, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2373849', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1934, 'n_words': 485}