Commit 9726a010 authored by Varun Patil's avatar Varun Patil

Fix notifications for Oreo

parent a5e845c3
...@@ -113,7 +113,8 @@ ...@@ -113,7 +113,8 @@
<service <service
android:name=".notifications.NotificationIntentService" android:name=".notifications.NotificationIntentService"
android:enabled="true" 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.NotificationEventReceiver" />
<receiver android:name=".notifications.NotificationServiceStarterReceiver"> <receiver android:name=".notifications.NotificationServiceStarterReceiver">
......
package app.insti; package app.insti;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
...@@ -70,6 +75,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -70,6 +75,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
initPicasso(); initPicasso();
} catch (IllegalStateException ignored) { } 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); setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext()); session = new SessionManager(getApplicationContext());
...@@ -98,8 +109,39 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -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) { private void handleIntent(Intent appLinkIntent) {
......
...@@ -2,16 +2,16 @@ package app.insti.notifications; ...@@ -2,16 +2,16 @@ package app.insti.notifications;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
public class 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_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
...@@ -57,7 +57,7 @@ NotificationEventReceiver extends WakefulBroadcastReceiver { ...@@ -57,7 +57,7 @@ NotificationEventReceiver extends WakefulBroadcastReceiver {
} }
if (serviceIntent != null) { if (serviceIntent != null) {
startWakefulService(context, serviceIntent); NotificationIntentService.enqueueWork(context, NotificationIntentService.class, 200, serviceIntent);
} }
} }
} }
\ No newline at end of file
package app.insti.notifications; package app.insti.notifications;
import android.app.IntentService;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
...@@ -8,8 +7,8 @@ import android.content.Context; ...@@ -8,8 +7,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log; import android.util.Log;
import java.util.Date; import java.util.Date;
...@@ -28,7 +27,7 @@ import retrofit2.Call; ...@@ -28,7 +27,7 @@ import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; 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_START = "ACTION_START";
private static final String ACTION_DELETE = "ACTION_DELETE"; private static final String ACTION_DELETE = "ACTION_DELETE";
...@@ -39,7 +38,7 @@ public class NotificationIntentService extends IntentService { ...@@ -39,7 +38,7 @@ public class NotificationIntentService extends IntentService {
private NotificationManager manager; private NotificationManager manager;
public NotificationIntentService() { public NotificationIntentService() {
super(NotificationIntentService.class.getSimpleName()); super();
} }
public static Intent createIntentStartNotificationService(Context context) { public static Intent createIntentStartNotificationService(Context context) {
...@@ -60,7 +59,7 @@ public class NotificationIntentService extends IntentService { ...@@ -60,7 +59,7 @@ public class NotificationIntentService extends IntentService {
} }
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleWork(Intent intent) {
Log.d(getClass().getSimpleName(), "onHandleIntent, started handling a notification event"); Log.d(getClass().getSimpleName(), "onHandleIntent, started handling a notification event");
try { try {
String action = intent.getAction(); String action = intent.getAction();
...@@ -98,9 +97,7 @@ public class NotificationIntentService extends IntentService { ...@@ -98,9 +97,7 @@ public class NotificationIntentService extends IntentService {
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
startActivity(mapIntent); startActivity(mapIntent);
} }
} finally { } finally { }
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
} }
private void processDeleteNotification(Intent intent) { private void processDeleteNotification(Intent intent) {
...@@ -122,9 +119,10 @@ public class NotificationIntentService extends IntentService { ...@@ -122,9 +119,10 @@ public class NotificationIntentService extends IntentService {
if (goingEventList != null) { if (goingEventList != null) {
for (Event event : goingEventList) { for (Event event : goingEventList) {
long timediff = getDateDiff(new Date(), event.getEventStartTime(), TimeUnit.MINUTES); 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(); 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()) builder.setContentTitle(event.getEventName())
.setAutoCancel(true) .setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent)) .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