Search in sources :

Example 1 with MonETSNotification

use of ca.etsmtl.applets.etsmobile.model.MonETSNotification in project ETSMobile-Android2 by ApplETS.

the class ETSGcmListenerService method sendNotification.

/**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param data GCM message received.
     */
private void sendNotification(Bundle 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 = getMonETSNotificationFromBundle(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);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
    NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
    String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
    inBoxStyle.setBigContentTitle(bigContentTitle);
    String username = ApplicationManager.userCredentials.getUsername();
    Spannable sb = new SpannableString(username);
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inBoxStyle.setSummaryText(sb);
    securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
    int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
    minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
    for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
        inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
    }
    if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
        int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
        String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
        Spannable others = new SpannableString(plusOthersString);
        others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inBoxStyle.addLine(others);
    }
    mBuilder.setStyle(inBoxStyle);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(android.app.NotificationManager) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) Bitmap(android.graphics.Bitmap) TypeToken(com.google.gson.reflect.TypeToken) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Spannable(android.text.Spannable)

Example 2 with MonETSNotification

use of ca.etsmtl.applets.etsmobile.model.MonETSNotification in project ETSMobile-Android2 by ApplETS.

the class ETSFcmListenerService method sendNotification.

/**
 * Create and show a simple notification containing the received FCM 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);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = mNotificationManager.getNotificationChannel(Constants.DEFAULT_NOTIFICATION_CHANNEL_ID);
        if (channel == null) {
            // We could create multiple channels based on the notification but let's just create one for maintenance purposes.
            String channelName = getString(R.string.fcm_fallback_notification_channel_label);
            channel = new NotificationChannel(Constants.DEFAULT_NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
        }
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Constants.DEFAULT_NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
    NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
    String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
    inBoxStyle.setBigContentTitle(bigContentTitle);
    String username = ApplicationManager.userCredentials.getUsername();
    Spannable sb = new SpannableString(username);
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inBoxStyle.setSummaryText(sb);
    securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
    int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
    minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
    for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
        inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
    }
    if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
        int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
        String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
        Spannable others = new SpannableString(plusOthersString);
        others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inBoxStyle.addLine(others);
    }
    mBuilder.setStyle(inBoxStyle);
    mNotificationManager.notify(1, mBuilder.build());
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(android.app.NotificationManager) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) SpannableString(android.text.SpannableString) NotificationChannel(android.app.NotificationChannel) SpannableString(android.text.SpannableString) Bitmap(android.graphics.Bitmap) TypeToken(com.google.gson.reflect.TypeToken) StyleSpan(android.text.style.StyleSpan) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Spannable(android.text.Spannable)

Example 3 with MonETSNotification

use of ca.etsmtl.applets.etsmobile.model.MonETSNotification in project ETSMobile-Android2 by ApplETS.

the class Utility method loadNotifications.

/**
     * Gets MonÉTS notifications and update DB
     * @param context
     * @param requestListener
     */
public static void loadNotifications(Context context, final RequestListener<Object> requestListener) {
    final SecurePreferences securePreferences = new SecurePreferences(context);
    final boolean allNotifsLoaded = securePreferences.getBoolean(Constants.ALL_NOTIFS_LOADED, false);
    MonETSNotificationsRequest monETSNotificationsRequest;
    if (!allNotifsLoaded) {
        monETSNotificationsRequest = new MonETSNotificationsRequest(context, false);
    } else {
        monETSNotificationsRequest = new MonETSNotificationsRequest(context, true);
    }
    final DataManager dataManager = DataManager.getInstance(context);
    dataManager.start();
    final DatabaseHelper databaseHelper = new DatabaseHelper(context);
    dataManager.sendRequest(monETSNotificationsRequest, new RequestListener<Object>() {

        @Override
        public void onRequestFailure(SpiceException spiceException) {
            requestListener.onRequestFailure(spiceException);
            dataManager.stop();
        }

        @Override
        public void onRequestSuccess(Object o) {
            if (o instanceof MonETSNotificationList) {
                try {
                    Dao<MonETSNotification, ?> dao = databaseHelper.getDao(MonETSNotification.class);
                    MonETSNotificationList list = (MonETSNotificationList) o;
                    for (MonETSNotification monETSNotification : list) {
                        dao.createOrUpdate(monETSNotification);
                    }
                    if (!allNotifsLoaded) {
                        securePreferences.edit().putBoolean(Constants.ALL_NOTIFS_LOADED, true).commit();
                    }
                    requestListener.onRequestSuccess(list);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            dataManager.stop();
        }
    });
}
Also used : MonETSNotificationList(ca.etsmtl.applets.etsmobile.model.MonETSNotificationList) MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) SQLException(java.sql.SQLException) DataManager(ca.etsmtl.applets.etsmobile.http.DataManager) Dao(com.j256.ormlite.dao.Dao) MonETSNotificationsRequest(ca.etsmtl.applets.etsmobile.http.MonETSNotificationsRequest) DatabaseHelper(ca.etsmtl.applets.etsmobile.db.DatabaseHelper)

Example 4 with MonETSNotification

use of ca.etsmtl.applets.etsmobile.model.MonETSNotification in project ETSMobile-Android2 by ApplETS.

the class ETSGcmListenerService method getMonETSNotificationFromBundle.

public MonETSNotification getMonETSNotificationFromBundle(Bundle data) {
    int id = Integer.valueOf(data.getString("Id"));
    String notificationTexte = data.getString("NotificationTexte");
    String notificationApplicationNom = data.getString("NotificationApplicationNom");
    //String notificationSigleCours = data.getString("NotificationSigleCours");
    String url = data.getString("Url");
    return new MonETSNotification(id, 0, notificationTexte, null, notificationApplicationNom, url);
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) SpannableString(android.text.SpannableString)

Example 5 with MonETSNotification

use of ca.etsmtl.applets.etsmobile.model.MonETSNotification in project ETSMobile-Android2 by ApplETS.

the class NotificationActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);
    securePreferences = new SecurePreferences(this);
    progressBar = (ProgressBar) findViewById(R.id.progressBar_notifications);
    listView = (ListView) findViewById(R.id.listView_notifications);
    progressBar.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.ets_red_fonce), PorterDuff.Mode.MULTIPLY);
    progressBar.setVisibility(View.VISIBLE);
    notificationsAdapter = new NotificationsAdapter(this, R.layout.row_notification, new ArrayList<>());
    listView.setAdapter(notificationsAdapter);
    listView.setOnItemClickListener((parent, view, position, id) -> {
        MonETSNotification item = notificationsAdapter.getItem(position);
        String url = item.getUrl();
        if (URLUtil.isValidUrl(url)) {
            Utility.openChromeCustomTabs(NotificationActivity.this, url);
        }
    });
    syncAdapterWithDB();
    DataManager datamanager = DataManager.getInstance(this);
    NotificationManager manager = new NotificationManager(this, datamanager.getMonETSService(), this);
    manager.updateNotifications();
}
Also used : NotificationsAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.NotificationsAdapter) MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(ca.etsmtl.applets.etsmobile.util.NotificationManager) ArrayList(java.util.ArrayList) DataManager(ca.etsmtl.applets.etsmobile.http.DataManager) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences)

Aggregations

MonETSNotification (ca.etsmtl.applets.etsmobile.model.MonETSNotification)8 SpannableString (android.text.SpannableString)4 SecurePreferences (ca.etsmtl.applets.etsmobile.util.SecurePreferences)3 NotificationManager (android.app.NotificationManager)2 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Bitmap (android.graphics.Bitmap)2 Spannable (android.text.Spannable)2 StyleSpan (android.text.style.StyleSpan)2 DatabaseHelper (ca.etsmtl.applets.etsmobile.db.DatabaseHelper)2 DataManager (ca.etsmtl.applets.etsmobile.http.DataManager)2 Gson (com.google.gson.Gson)2 TypeToken (com.google.gson.reflect.TypeToken)2 SQLException (java.sql.SQLException)2 NotificationChannel (android.app.NotificationChannel)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 NotificationCompat (androidx.core.app.NotificationCompat)1 MonETSNotificationsRequest (ca.etsmtl.applets.etsmobile.http.MonETSNotificationsRequest)1 MonETSNotificationList (ca.etsmtl.applets.etsmobile.model.MonETSNotificationList)1 NotificationsAdapter (ca.etsmtl.applets.etsmobile.ui.adapter.NotificationsAdapter)1