Commit d507d6c2 authored by lvarano's avatar lvarano
Browse files

Fix Notification Error with API 28, Reboot Alarm Set, Last change check

Fixing the Error where Notifications are not shown in Android 8.0, Set alarm if the deivce is rebooted, Set last change check time in settings and use it for API request, enable or disable projection option in Request
parent a5797a4d
Pipeline #8492 passed with stages
in 2 minutes and 46 seconds
......@@ -131,7 +131,17 @@
android:parentActivityName=".core.MainActivity"
android:screenOrientation="portrait" />
<receiver android:name=".core.AlarmReceiver"/>
<receiver android:name=".core.AlarmBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
\ No newline at end of file
package ch.amiv.android_app.core;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Notifications.set_Alarm(context);
}
}
}
......@@ -18,6 +18,7 @@ public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Notifications.notify(context,"Alarm","Receiver called",R.drawable.ic_amiv_logo_icon);
Request.FetchEventListChanges(context, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
......@@ -28,7 +29,7 @@ public class AlarmReceiver extends BroadcastReceiver {
public void OnDataReceived() {
}
}, "2018-09-06T10:00:00Z");
}, Settings.GetPref(Settings.last_change_check_dateKey,context),false);
// TODO test if notification needed
......
......@@ -2,9 +2,11 @@ package ch.amiv.android_app.core;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
......@@ -89,6 +91,20 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
instance = this;
setContentView(R.layout.core_main);
// Create the NotificationChannel, but only on API 26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "1";
String description = "Notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
/*
//Use this to set a custom taskDescription in the app overview, ie when switching apps. Can set the icon, label and color of the bar
Resources r = getResources();
......@@ -124,7 +140,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
//Notifications.event_notifier(this,null);
Request.FetchEventListChanges(this, new Request.OnDataReceivedCallback() {
/*Request.FetchEventListChanges(this, new Request.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
......@@ -134,7 +150,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public void OnDataReceived() {
}
}, "2018-09-06T10:00:00Z");
}, "2018-09-06T10:00:00Z");*/
......
......@@ -5,6 +5,8 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.support.v4.app.NotificationCompat;
import org.json.JSONArray;
......@@ -12,6 +14,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.Calendar;
import java.util.Date;
import ch.amiv.android_app.R;
import ch.amiv.android_app.events.EventDetailActivity;
......@@ -29,8 +32,8 @@ public final class Notifications {
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.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
intent = new Intent(context, AlarmReceiver.class);
......@@ -64,6 +67,7 @@ public final class Notifications {
* @param icon icon
* @return generates notification on screen
*/
// not working with Android 8.0
public static void notify (Context context, String title, String text, int icon){
NotificationManager notificationManager = (NotificationManager) context
......@@ -121,12 +125,31 @@ public final class Notifications {
Notifications.notify_pending(context, "New Event", "take a look", R.drawable.ic_amiv_logo_icon, pendingIntent);
}
// TODO catch when projections are used
// TODO catch if no new events
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);
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);
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", event_id, R.drawable.ic_amiv_logo_icon, pendingIntent);
}
String dat = Request.dateFormat.format(Calendar.getInstance().getTime());
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){
//RunCallback(errorCallback);
......
......@@ -85,14 +85,20 @@ public final class Request {
* @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)
public static void FetchEventListChanges(final Context context, final OnDataReceivedCallback callback, final OnDataReceivedCallback errorCallback, @NonNull final String last_update_time, boolean projection)
{
if(!CheckConnection(context)) {
RunCallback(errorCallback);
return;
}
String url = Settings.API_URL + "events?" + "projection={\"_id\":1}" + "&where={\"_created\":{\"$gt\":\"" +last_update_time + "\"}, \"show_website\": true}";
String url;
if (projection) {
url = Settings.API_URL + "events?" + "projection={\"_id\":1}" + "&where={\"_created\":{\"$gt\":\"" + last_update_time + "\"}, \"show_website\": true}";
}
else{
url = Settings.API_URL + "events?" + "where={\"_created\":{\"$gt\":\"" + last_update_time + "\"}, \"show_website\": true}";
}
Log.e("request", "url: " + url);
StringRequest request = new StringRequest(com.android.volley.Request.Method.GET, url,null, null)
......@@ -140,7 +146,7 @@ public final class Request {
RunCallback(errorCallback);
return super.parseNetworkError(volleyError);
}
/*
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
if(Settings.HasToken(context)) {
......@@ -154,7 +160,7 @@ public final class Request {
}
return super.getHeaders();
}*/
}
};
//send the request and check if it failed
......
......@@ -43,6 +43,9 @@ public class Settings {
public static final Pair<String, Float> checkin_refreshRate = new Pair<>("checkin.refreshfrequency", 20f);
//endregion
//last changes check
public static final String[] last_change_check_dateKey = {"core.notifications_date", "1979-02-19T10:00:00Z"};
//region ---SharedPrefs---
/**
* Will check that the shared prefs instance is set so we can edit/retrieve values
......
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