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