Skip to content

Commit

Permalink
Fix: Notification doesn't work with target SDK 27+ Etar-Group#372
Browse files Browse the repository at this point in the history
  • Loading branch information
xsoh authored and jspricke committed Sep 29, 2018
1 parent e6b33d7 commit d120b50
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/com/android/calendar/alerts/AlertReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

import ws.xsoh.etar.R;

import static com.android.calendar.alerts.AlertService.ALERT_CHANNEL_ID;

/**
* Receives android.intent.action.EVENT_REMINDER intents and handles
* event reminders. The intent URI specifies an alert id in the
Expand Down Expand Up @@ -258,6 +260,13 @@ private static Notification buildBasicNotification(Notification.Builder notifica
notificationBuilder.setSmallIcon(R.drawable.stat_notify_calendar);
notificationBuilder.setContentIntent(clickIntent);
notificationBuilder.setDeleteIntent(deleteIntent);

// Add setting channel ID for Oreo or later
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(ALERT_CHANNEL_ID);
}


if (doPopup) {
notificationBuilder.setFullScreenIntent(createAlertActivityIntent(context), true);
}
Expand Down Expand Up @@ -319,7 +328,7 @@ private static Notification buildBasicNotification(Notification.Builder notifica
resources.getString(R.string.snooze_label), snoozeIntent);
numActions++;
}
return notificationBuilder.getNotification();
return notificationBuilder.build();

} else {
// Old-style notification (pre-JB). Use custom view with buttons to provide
Expand Down
20 changes: 20 additions & 0 deletions src/com/android/calendar/alerts/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.android.calendar.alerts;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ContentResolver;
Expand Down Expand Up @@ -61,6 +63,9 @@
* This service is used to handle calendar event reminders.
*/
public class AlertService extends Service {

public static final String ALERT_CHANNEL_ID = "alert_channel_01";// The id of the channel.

// Hard limit to the number of notifications displayed.
public static final int MAX_NOTIFICATIONS = 20;
static final boolean DEBUG = true;
Expand Down Expand Up @@ -149,6 +154,13 @@ static boolean updateAlertNotification(Context context) {
ContentResolver cr = context.getContentResolver();
NotificationMgr nm = new NotificationMgrWrapper(
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String appName = context.getString(R.string.standalone_app_label);
NotificationChannel channel = new NotificationChannel(ALERT_CHANNEL_ID, appName, NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(channel);
}

final long currentTime = System.currentTimeMillis();
SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);

Expand Down Expand Up @@ -1047,10 +1059,18 @@ public void cancel(int id) {
mNm.cancel(id);
}

@TargetApi(Build.VERSION_CODES.O)
@Override
public void createNotificationChannel(NotificationChannel channel) {
mNm.createNotificationChannel(channel);
}

@Override
public void notify(int id, NotificationWrapper nw) {
mNm.notify(id, nw.mNotification);
}


}

static class NotificationInfo {
Expand Down
3 changes: 3 additions & 0 deletions src/com/android/calendar/alerts/NotificationMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.android.calendar.alerts;

import android.app.NotificationChannel;

import com.android.calendar.alerts.AlertService.NotificationWrapper;

public abstract class NotificationMgr {
public abstract void notify(int id, NotificationWrapper notification);
public abstract void cancel(int id);
public abstract void createNotificationChannel(NotificationChannel channel);

/**
* Don't actually use the notification framework's cancelAll since the SyncAdapter
Expand Down

0 comments on commit d120b50

Please sign in to comment.