From d0683679f21a2d431d33fb985a72123c37cf7508 Mon Sep 17 00:00:00 2001 From: Suhail Alkowaileet Date: Fri, 28 Sep 2018 12:10:18 +0300 Subject: [PATCH] Fix: Notification doesn't work with target SDK 27+ #372 --- .../android/calendar/alerts/AlertReceiver.java | 10 +++++++++- .../android/calendar/alerts/AlertService.java | 18 ++++++++++++++++++ .../calendar/alerts/NotificationMgr.java | 3 +++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/com/android/calendar/alerts/AlertReceiver.java b/src/com/android/calendar/alerts/AlertReceiver.java index 4891e763d..dfc81864d 100644 --- a/src/com/android/calendar/alerts/AlertReceiver.java +++ b/src/com/android/calendar/alerts/AlertReceiver.java @@ -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 @@ -258,6 +260,12 @@ 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); } @@ -319,7 +327,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 diff --git a/src/com/android/calendar/alerts/AlertService.java b/src/com/android/calendar/alerts/AlertService.java index 469ac3bec..c5f9af89d 100644 --- a/src/com/android/calendar/alerts/AlertService.java +++ b/src/com/android/calendar/alerts/AlertService.java @@ -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; @@ -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; @@ -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); @@ -1047,6 +1059,12 @@ 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); diff --git a/src/com/android/calendar/alerts/NotificationMgr.java b/src/com/android/calendar/alerts/NotificationMgr.java index 0ab475c3c..718f20321 100644 --- a/src/com/android/calendar/alerts/NotificationMgr.java +++ b/src/com/android/calendar/alerts/NotificationMgr.java @@ -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