Commit 3e7165e6 authored by lvarano's avatar lvarano
Browse files

notifier to handle multiple events implemented, comments and formatting

Notification and Alarm classes are commented and formatted, handle for multiple new events added
parent de1c760b
Pipeline #8526 passed with stages
in 2 minutes and 10 seconds
package ch.amiv.android_app.core; package ch.amiv.android_app.core;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import ch.amiv.android_app.R;
import ch.amiv.android_app.events.EventDetailActivity;
public class AlarmReceiver extends BroadcastReceiver { public class AlarmReceiver extends BroadcastReceiver {
int notification_id=0; int notification_id=0;
String last_check_time;
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Notifications.notify(context,"Alarm","Receiver called",R.drawable.ic_amiv_logo_icon); //Notifications.notify(context,"Alarm","Receiver called",R.drawable.ic_amiv_logo_icon);
Request.FetchEventListChanges(context, new Request.OnDataReceivedCallback() { Request.FetchEventListChanges(context, new Request.OnDataReceivedCallback() {
@Override @Override
public void OnDataReceived() { public void OnDataReceived() {
...@@ -29,7 +23,8 @@ public class AlarmReceiver extends BroadcastReceiver { ...@@ -29,7 +23,8 @@ public class AlarmReceiver extends BroadcastReceiver {
public void OnDataReceived() { public void OnDataReceived() {
} }
}, Settings.GetPref(Settings.last_change_check_dateKey,context),false); }, "2018-05-06T10:00:00Z",false); // TODO change date here to use last checked date
// use Settings.GetPref(Settings.last_change_check_dateKey,context)
// TODO test if notification needed // TODO test if notification needed
......
...@@ -55,6 +55,7 @@ public class ListFragment extends Fragment { ...@@ -55,6 +55,7 @@ public class ListFragment extends Fragment {
public void OnDataReceived() { public void OnDataReceived() {
Request.FetchEventSignups(MainActivity.instance, onSignupsUpdatedCallback, null, ""); Request.FetchEventSignups(MainActivity.instance, onSignupsUpdatedCallback, null, "");
RefreshList(true); RefreshList(true);
} }
}; };
......
package ch.amiv.android_app.core; package ch.amiv.android_app.core;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
...@@ -14,7 +12,6 @@ import android.support.design.widget.NavigationView; ...@@ -14,7 +12,6 @@ import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NotificationCompat;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
...@@ -37,7 +34,6 @@ import ch.amiv.android_app.jobs.JobDetailActivity; ...@@ -37,7 +34,6 @@ import ch.amiv.android_app.jobs.JobDetailActivity;
import ch.amiv.android_app.util.PersistentStorage; import ch.amiv.android_app.util.PersistentStorage;
import ch.amiv.android_app.util.Util; import ch.amiv.android_app.util.Util;
import static ch.amiv.android_app.core.Notifications.pendingIntent;
/** /**
* This is the first screen. features: drawer, pageview with bottom navigation bar and within each page a list view. * This is the first screen. features: drawer, pageview with bottom navigation bar and within each page a list view.
...@@ -91,15 +87,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -91,15 +87,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
instance = this; instance = this;
setContentView(R.layout.core_main); setContentView(R.layout.core_main);
// Create the NotificationChannel, but only on API 26+ // Create the NotificationChannel to run notifications on API 26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "1"; CharSequence name = "1";
String description = "Notifications"; String description = "Notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT; int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("1", name, importance); NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description); channel.setDescription(description);
// Register the channel with the system; you can't change the importance // Register the channel in the system
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class); NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
} }
...@@ -132,28 +127,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -132,28 +127,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
PersistentStorage.LoadJobs(getApplicationContext()); PersistentStorage.LoadJobs(getApplicationContext());
InitialisePageView(); InitialisePageView();
// send test notification at start // TODO check if notifications are enabled in the settings
//Notifications.notify(this,"new Event up", "register now", R.drawable.ic_amiv_logo_icon);
Notifications.set_Alarm(this); Notifications.set_Alarm(this);
//Notifications.event_notifier(this,null);
/*Request.FetchEventListChanges(this, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
}
}, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
}
}, "2018-09-06T10:00:00Z");*/
//fetch the user info if we are logged in, there exists a token from the previous session, should be cached. //fetch the user info if we are logged in, there exists a token from the previous session, should be cached.
if(!PersistentStorage.LoadUserInfo(getApplicationContext()) || UserInfo.current._id.isEmpty() && !Settings.IsEmailOnlyLogin(getApplicationContext())) { if(!PersistentStorage.LoadUserInfo(getApplicationContext()) || UserInfo.current._id.isEmpty() && !Settings.IsEmailOnlyLogin(getApplicationContext())) {
Request.FetchUserData(getApplicationContext(), drawerNav, new Request.OnDataReceivedCallback() { Request.FetchUserData(getApplicationContext(), drawerNav, new Request.OnDataReceivedCallback() {
......
...@@ -5,8 +5,6 @@ import android.app.NotificationManager; ...@@ -5,8 +5,6 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import org.json.JSONArray; import org.json.JSONArray;
...@@ -14,49 +12,42 @@ import org.json.JSONException; ...@@ -14,49 +12,42 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import ch.amiv.android_app.R; import ch.amiv.android_app.R;
import ch.amiv.android_app.events.EventDetailActivity; import ch.amiv.android_app.events.EventDetailActivity;
import ch.amiv.android_app.events.Events;
public final class Notifications { public final class Notifications {
static AlarmManager alarm; public static AlarmManager alarm;
static Intent intent; public static Intent intent;
static PendingIntent pendingIntent; public static PendingIntent pendingIntent;
/** /**
* *
* @param context * @param context
* @return sets daily alarm to 18:00 * @return sets daily alarm to XX:XX
*/ */
static void set_Alarm (Context context){ static void set_Alarm (Context context){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 2); // set alarm time in calendar
calendar.set(Calendar.MINUTE, 50); Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 23);
calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.SECOND, 0);
// pending intent to activate activity when notification is clicked
intent = new Intent(context, AlarmReceiver.class); intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast( pendingIntent = PendingIntent.getBroadcast(
context, 0, intent, context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT); // updates intent if it already exists PendingIntent.FLAG_UPDATE_CURRENT); // updates intent if it already exists
// set alarm manager
alarm = (AlarmManager) context alarm = (AlarmManager) context
.getSystemService(context.ALARM_SERVICE); .getSystemService(context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent); AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
static boolean is_alarm_set (){
AlarmManager.AlarmClockInfo ala = alarm.getNextAlarmClock();
if(ala!= null)
return true;
return false;
} }
/** /**
...@@ -64,20 +55,21 @@ public final class Notifications { ...@@ -64,20 +55,21 @@ public final class Notifications {
* @param context * @param context
* @param title title of notification * @param title title of notification
* @param text text of notification * @param text text of notification
* @param icon icon * @param icon icon of notification
* @return generates notification on screen * @return immediately generates notification on screen
*/ */
// not working with Android 8.0
public static void notify (Context context, String title, String text, int icon){ public static void notify (Context context, String title, String text, int icon){
NotificationManager notificationManager = (NotificationManager) context NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder( NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context,"1").setSmallIcon(icon) // channel id for compatibility with newer android versions context,"1")
.setSmallIcon(icon)
.setContentTitle(title) .setContentTitle(title)
.setContentText(text) .setContentText(text)
.setAutoCancel(true); .setAutoCancel(true);
notificationManager.notify(0, mNotifyBuilder.build()); notificationManager.notify(0, mNotifyBuilder.build());
} }
...@@ -88,7 +80,8 @@ public final class Notifications { ...@@ -88,7 +80,8 @@ public final class Notifications {
* @param title title of notification * @param title title of notification
* @param text text of notification * @param text text of notification
* @param icon icon * @param icon icon
* @param pendingIntent generates notification on screen which starts the pending activity onClick * @param pendingIntent pending intent to activate if notification is clicked
* @return generates notification on screen which starts the pending activity onClick
*/ */
public static void notify_pending (Context context, String title, String text, int icon, PendingIntent pendingIntent){ public static void notify_pending (Context context, String title, String text, int icon, PendingIntent pendingIntent){
...@@ -96,10 +89,14 @@ public final class Notifications { ...@@ -96,10 +89,14 @@ public final class Notifications {
.getSystemService(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder( NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context,"1").setSmallIcon(icon) // channel id for compatibility with newer android versions context,"1")
.setSmallIcon(icon)
.setContentTitle(title) .setContentTitle(title)
.setContentText(text) .setContentText(text)
.setAutoCancel(true).setContentIntent(pendingIntent); .setColor(context.getResources().getColor(R.color.primary)).setColorized(true)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationManager.notify(0, mNotifyBuilder.build()); notificationManager.notify(0, mNotifyBuilder.build());
} }
...@@ -107,57 +104,91 @@ public final class Notifications { ...@@ -107,57 +104,91 @@ public final class Notifications {
/** /**
* *
* @param context * @param context
* @param json JSON with all new events
* @return event notificiation with appropriate onClick event
*/ */
// TODO
public static void event_notifier (Context context, JSONObject json){ public static void event_notifier (Context context, JSONObject json){
if(json == null) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EventDetailActivity.class);
notificationIntent.putExtra("eventGroup", 1);
notificationIntent.putExtra("eventIndex", 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notifications.notify_pending(context, "New Event", "take a look", R.drawable.ic_amiv_logo_icon, pendingIntent);
}
// TODO catch when projections are used // TODO catch when projections are used
// TODO catch if no new events
else{
try{ try{
JSONArray items = json.getJSONArray("_items");
if(items.length()!=0) {
JSONObject event = items.getJSONObject(0);
String event_id = (String) event.get("title_en");
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EventDetailActivity.class); JSONArray items = json.getJSONArray("_items");
notificationIntent.putExtra("eventGroup", 1);
notificationIntent.putExtra("eventIndex", 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, if(items.length()!=0) {
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notifications.notify_pending(context, "New Event", event_id, R.drawable.ic_amiv_logo_icon, pendingIntent); // for one new event -> notify and onClick show details
if(items.length()==1) {
// get event from JSONObject and add it to events list
JSONObject event = items.getJSONObject(0);
Events.AddEvent(event);
// refetch event list to add new event
String event_id = (String) event.get("_id");
Request.FetchEventList(context, null, null, event_id);
// generate pending intent to get EventDetails onClick
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EventDetailActivity.class);
notificationIntent.putExtra(EventDetailActivity.LauncherExtras.EVENT_ID, event_id);
notificationIntent.putExtra(EventDetailActivity.LauncherExtras.LOAD_EVENTS, true);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// generate notification
String title = (String) event.get("title_de");
Notifications.notify_pending(context, "New Event added", title, R.drawable.ic_amiv_logo_icon, pendingIntent);
}
// multiple new events -> show all titles and onClick show events main page
else{
// get event id's from new events
String [] event_id = new String[items.length()];
for(int i = 0; i<items.length();i++){
JSONObject event = items.getJSONObject(i);
Events.AddEvent(event); // TODO efficiency !
event_id[i]= (String) event.get("_id");
}
// add new events to list
// TODO how -> new function?
Request.FetchEventList(context, null, null,event_id[0]); // TODO not null !
// onClick start main activity
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// build string to display all event titles
StringBuilder titles_list = new StringBuilder();
titles_list.append ((String)items.getJSONObject(0).get("title_de"));
for(int j = 1; j<items.length();j++){
titles_list.append(", ");
titles_list.append((String)items.getJSONObject(j).get("title_de"));
}
Notifications.notify_pending(context, "Many new Events added", titles_list.toString(), R.drawable.ic_amiv_logo_icon, pendingIntent);
}
} }
// sets last change check to current time
String dat = Request.dateFormat.format(Calendar.getInstance().getTime()); String dat = Request.dateFormat.format(Calendar.getInstance().getTime());
Settings.SetPref(Settings.last_change_check_dateKey,dat,context); Settings.SetPref(Settings.last_change_check_dateKey,dat,context);
//notify(context,"Change time",Settings.GetPref(Settings.last_change_check_dateKey,context),R.drawable.ic_amiv_logo_icon);
}catch(JSONException ex){ }catch(JSONException ex){
//RunCallback(errorCallback); // TODO
//e.printStackTrace();
} }
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment