From 3f56868998f50e8e45107e792bcba849e164f3f6 Mon Sep 17 00:00:00 2001 From: Boris Timofeev Date: Fri, 1 Sep 2017 18:12:07 +0300 Subject: [PATCH] support notification channels from Android O --- .../org/emunix/unipatcher/UniPatcher.java | 22 +++++++++++++++++++ .../org/emunix/unipatcher/WorkerService.java | 2 +- .../emunix/unipatcher/ui/notify/Notify.java | 4 ++-- app/src/main/res/values/strings.xml | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/emunix/unipatcher/UniPatcher.java b/app/src/main/java/org/emunix/unipatcher/UniPatcher.java index 03c5aa5..f26ace3 100644 --- a/app/src/main/java/org/emunix/unipatcher/UniPatcher.java +++ b/app/src/main/java/org/emunix/unipatcher/UniPatcher.java @@ -21,9 +21,20 @@ package org.emunix.unipatcher; import android.app.Application; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.os.Build; public class UniPatcher extends Application { private static String appArgument = null; + public static final String NOTIFICATION_CHANNEL_ID = "notifications"; + + @Override + public void onCreate() { + super.onCreate(); + initNotificationChannel(); + } public static String getAppArgument() { return appArgument; @@ -32,4 +43,15 @@ public class UniPatcher extends Application { public static void setAppArgument(String appArgument) { UniPatcher.appArgument = appArgument; } + + public void initNotificationChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return; + } + NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, + getString(R.string.notification_channel_name), + NotificationManager.IMPORTANCE_DEFAULT); + manager.createNotificationChannel(channel); + } } diff --git a/app/src/main/java/org/emunix/unipatcher/WorkerService.java b/app/src/main/java/org/emunix/unipatcher/WorkerService.java index 2de0874..c689e83 100644 --- a/app/src/main/java/org/emunix/unipatcher/WorkerService.java +++ b/app/src/main/java/org/emunix/unipatcher/WorkerService.java @@ -326,7 +326,7 @@ public class WorkerService extends IntentService { private void showErrorNotification(String text) { Intent notificationIntent = new Intent(this, MainActivity.class); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - Notification notify = new NotificationCompat.Builder(this) + Notification notify = new NotificationCompat.Builder(this, UniPatcher.NOTIFICATION_CHANNEL_ID) .setContentTitle(getString(R.string.notify_error)) .setContentText(text) .setSmallIcon(R.drawable.ic_stat_patching) diff --git a/app/src/main/java/org/emunix/unipatcher/ui/notify/Notify.java b/app/src/main/java/org/emunix/unipatcher/ui/notify/Notify.java index 94804d5..f51902c 100644 --- a/app/src/main/java/org/emunix/unipatcher/ui/notify/Notify.java +++ b/app/src/main/java/org/emunix/unipatcher/ui/notify/Notify.java @@ -24,6 +24,7 @@ import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; +import org.emunix.unipatcher.UniPatcher; import org.emunix.unipatcher.ui.activity.MainActivity; public abstract class Notify { @@ -40,9 +41,8 @@ public abstract class Notify { Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); - notifyBuilder = new NotificationCompat.Builder(context); + notifyBuilder = new NotificationCompat.Builder(context, UniPatcher.NOTIFICATION_CHANNEL_ID); notifyBuilder.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT)); - //notifyMng = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notifyMng = NotificationManagerCompat.from(context); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a35444f..e8d96c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -167,6 +167,7 @@ Donation icon + Notifications