Commit 90a8b75f authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #159 from pulsejet/on

Fix notifications for Oreo
parents 686a1c48 9726a010
......@@ -113,7 +113,8 @@
<service
android:name=".notifications.NotificationIntentService"
android:enabled="true"
android:exported="false" />
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver android:name=".notifications.NotificationEventReceiver" />
<receiver android:name=".notifications.NotificationServiceStarterReceiver">
......
package app.insti;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
......@@ -70,6 +75,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
initPicasso();
} catch (IllegalStateException ignored) {
}
/* Make notification channel on oreo */
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
createNotificationChannel();
}
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
......@@ -98,8 +109,39 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O)
NotificationEventReceiver.setupAlarm(getApplicationContext());
NotificationEventReceiver.setupAlarm(getApplicationContext());
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "INSTIAPP_CHANNEL";
// The user-visible name of the channel.
CharSequence name = "InstiApp";
// The user-visible description of the channel.
String description = "InstiApp Notifications";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = null;
mChannel = new NotificationChannel(id, name,importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
}
private void handleIntent(Intent appLinkIntent) {
......
......@@ -2,16 +2,16 @@ package app.insti.notifications;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import java.util.Calendar;
import java.util.Date;
public class
NotificationEventReceiver extends WakefulBroadcastReceiver {
NotificationEventReceiver extends BroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
......@@ -57,7 +57,7 @@ NotificationEventReceiver extends WakefulBroadcastReceiver {
}
if (serviceIntent != null) {
startWakefulService(context, serviceIntent);
NotificationIntentService.enqueueWork(context, NotificationIntentService.class, 200, serviceIntent);
}
}
}
\ No newline at end of file
package app.insti.notifications;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
......@@ -8,8 +7,8 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import java.util.Date;
......@@ -28,7 +27,7 @@ import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class NotificationIntentService extends IntentService {
public class NotificationIntentService extends JobIntentService {
private static final String ACTION_START = "ACTION_START";
private static final String ACTION_DELETE = "ACTION_DELETE";
......@@ -39,7 +38,7 @@ public class NotificationIntentService extends IntentService {
private NotificationManager manager;
public NotificationIntentService() {
super(NotificationIntentService.class.getSimpleName());
super();
}
public static Intent createIntentStartNotificationService(Context context) {
......@@ -60,7 +59,7 @@ public class NotificationIntentService extends IntentService {
}
@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(Intent intent) {
Log.d(getClass().getSimpleName(), "onHandleIntent, started handling a notification event");
try {
String action = intent.getAction();
......@@ -98,9 +97,7 @@ public class NotificationIntentService extends IntentService {
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
startActivity(mapIntent);
}
} finally {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
} finally { }
}
private void processDeleteNotification(Intent intent) {
......@@ -122,9 +119,10 @@ public class NotificationIntentService extends IntentService {
if (goingEventList != null) {
for (Event event : goingEventList) {
long timediff = getDateDiff(new Date(), event.getEventStartTime(), TimeUnit.MINUTES);
if (timediff <= 30 && timediff > 0) { // Change this to 30*10000 for testing
if (timediff <= 50 && timediff > 0) { // Change this to 30*10000 for testing
NOTIFICATION_ID = event.getEventID().hashCode();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "INSTIAPP_CHANNEL");
builder.setContentTitle(event.getEventName())
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
......
Markdown is supported
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