Commit f855239e authored by lvarano's avatar lvarano
Browse files

Notifications helper classes

Added class to manage alarms and notifications 
Additional Request function to find changes in events list
parent 44c02427
Pipeline #8452 failed with stages
in 2 minutes and 47 seconds
......@@ -131,5 +131,7 @@
android:parentActivityName=".core.MainActivity"
android:screenOrientation="portrait" />
<receiver android:name=".core.AlarmReceiver"/>
</application>
</manifest>
\ No newline at end of file
package ch.amiv.android_app.core;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
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 {
int notification_id=0;
String last_check_time;
@Override
public void onReceive(Context context, Intent intent) {
Request.FetchEventListChanges(context, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
}
}, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
}
}, "2018-09-06T10:00:00Z");
// TODO test if notification needed
// TODO enter event notifier here
notification_id++;
}
}
package ch.amiv.android_app.core;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Calendar;
import ch.amiv.android_app.R;
import ch.amiv.android_app.events.EventDetailActivity;
public final class Notifications {
static AlarmManager alarm;
static Intent intent;
static PendingIntent pendingIntent;
/**
*
* @param context
* @return sets daily alarm to 18:00
*/
static void set_Alarm (Context context){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT); // updates intent if it already exists
alarm = (AlarmManager) context
.getSystemService(context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
static boolean is_alarm_set (){
AlarmManager.AlarmClockInfo ala = alarm.getNextAlarmClock();
if(ala!= null)
return true;
return false;
}
/**
*
* @param context
* @param title title of notification
* @param text text of notification
* @param icon icon
* @return generates notification on screen
*/
public static void notify (Context context, String title, String text, int icon){
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context,"1").setSmallIcon(icon) // channel id for compatibility with newer android versions
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(true);
notificationManager.notify(0, mNotifyBuilder.build());
}
/**
*
* @param context
* @param title title of notification
* @param text text of notification
* @param icon icon
* @param pendingIntent 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){
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context,"1").setSmallIcon(icon) // channel id for compatibility with newer android versions
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(true).setContentIntent(pendingIntent);
notificationManager.notify(0, mNotifyBuilder.build());
}
/**
*
* @param context
*/
// TODO
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);
}
else{
try{
JSONArray items = json.getJSONArray("_items");
JSONObject event = items.getJSONObject(0);
String event_id = (String)event.get("_id");
notify(context,"event found",event_id,R.drawable.ic_amiv_logo_icon);
}catch(JSONException ex){
//RunCallback(errorCallback);
//e.printStackTrace();
}
}
}
}
......@@ -80,6 +80,88 @@ public final class Request {
return true;
}
/**
* Will fetch the list of events from the server, note does not require an access token.
* @param errorCallback Use this to know when an error occurred to stop loading animations etc
* @param last_update_time last changes check time
*/
public static void FetchEventListChanges(final Context context, final OnDataReceivedCallback callback, final OnDataReceivedCallback errorCallback, @NonNull final String last_update_time)
{
if(!CheckConnection(context)) {
RunCallback(errorCallback);
return;
}
String url = Settings.API_URL + "events?" + "projection={\"_id\":1}" + "&where={\"_created\":{\"$gt\":" + + "}, \"show_website\": true}";
Log.e("request", "url: " + url);
StringRequest request = new StringRequest(com.android.volley.Request.Method.GET, url,null, null)
{
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) { //Note: the parseNetworkResponse is only called if the response was successful (codes 2xx), else parseNetworkError is called.
if(response != null) {
Log.e("request", "fetch events status Code: " + response.statusCode);
try {
final JSONObject json = new JSONObject(new String(response.data));
//Update events on main thread
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO alarmreceiver
Notifications.event_notifier(context,json);
if(callback != null)
callback.OnDataReceived();
}
};
callbackHandler.post(runnable);
Log.e("request", new JSONObject(new String(response.data)).toString());
} catch (JSONException e) {
RunCallback(errorCallback);
e.printStackTrace();
}
}
else {
RunCallback(errorCallback);
Log.e("request", "Request returned null response. fetch events");
}
return super.parseNetworkResponse(response);
}
@Override
protected VolleyError parseNetworkError(final VolleyError volleyError) { //see comments at parseNetworkResponse()
if(volleyError != null && volleyError.networkResponse != null)
Log.e("request", "status code: " + volleyError.networkResponse.statusCode + "\n" + new String(volleyError.networkResponse.data));
else
Log.e("request", "Request returned null response. fetch events");
RunCallback(errorCallback);
return super.parseNetworkError(volleyError);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
if(Settings.HasToken(context)) {
Map<String,String> headers = new HashMap<String, String>();
String credentials = Settings.GetToken(context) + ":";
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Authorization", auth);
return headers;
}
return super.getHeaders();
}
};
//send the request and check if it failed
if(!Request.SendRequest(request, context))
RunCallback(errorCallback);
}
/**
* Will fetch the list of events from the server, note does not require an access token.
* @param errorCallback Use this to know when an error occurred to stop loading animations etc
......
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