From c7529693d8f7d31c9530d0203ceca15850b5c1bd Mon Sep 17 00:00:00 2001 From: Eugene Maksymenko Date: Thu, 6 Dec 2018 19:57:57 +0200 Subject: [PATCH] Fix notification appearance in Android O. --- app/build.gradle | 14 +-- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- library/build.gradle | 14 +-- .../NotificationHelper.java | 90 +++++++++++-------- library/src/main/res/values/strings.xml | 1 + 6 files changed, 72 insertions(+), 55 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9e4fc5a..eaf5df9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 28 + buildToolsVersion '28.0.3' defaultConfig { applicationId "com.permissioneverywhere.demo" minSdkVersion 15 - targetSdkVersion 23 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -20,8 +20,8 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.3.0' - compile 'com.github.kaknazaveshtakipishi:PermissionEverywhere:alpha' + implementation fileTree(include: ['*.jar'], dir: 'libs') + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.github.kaknazaveshtakipishi:PermissionEverywhere:1.0.2' } diff --git a/build.gradle b/build.gradle index e54f459..1e12f8f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'com.android.tools.build:gradle:3.2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -16,6 +17,7 @@ allprojects { repositories { maven { url "https://jitpack.io" } jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 122a0dc..535969f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Dec 28 10:00:20 PST 2015 +#Thu Dec 06 19:44:26 EET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip diff --git a/library/build.gradle b/library/build.gradle index 7e14fb3..dbecc8b 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 28 + buildToolsVersion "28.0.3" defaultConfig { - minSdkVersion 11 - targetSdkVersion 23 + minSdkVersion 14 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -19,7 +19,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.3.0' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:28.0.0' } diff --git a/library/src/main/java/com/permissioneverywhere/NotificationHelper.java b/library/src/main/java/com/permissioneverywhere/NotificationHelper.java index b9ff79b..a35b187 100644 --- a/library/src/main/java/com/permissioneverywhere/NotificationHelper.java +++ b/library/src/main/java/com/permissioneverywhere/NotificationHelper.java @@ -1,52 +1,66 @@ package com.permissioneverywhere; -import android.Manifest; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; +import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.os.ResultReceiver; +class NotificationHelper { + public static final int REQUEST_CODE_PUSH = 77; - class NotificationHelper { - public static final int REQUEST_CODE_PUSH = 77; - - public static void sendNotification(Context context, - String[] permissions, - int requestCode, - String notificationTitle, - String notificationText, - int notificationIcon, - ResultReceiver receiver) { - - Intent intent = new Intent(context, PermissionActivity.class); - intent.putExtra(Const.REQUEST_CODE, requestCode); - intent.putExtra(Const.PERMISSIONS_ARRAY, permissions); - intent.putExtra(Const.RESULT_RECEIVER, receiver); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); - - PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_PUSH, intent, - PendingIntent.FLAG_ONE_SHOT); - - Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) - .setSmallIcon(notificationIcon) - .setContentTitle(notificationTitle) - .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText)) - .setContentText(notificationText) - .setAutoCancel(true) - .setSound(defaultSoundUri) - .setPriority(Notification.PRIORITY_HIGH) - .setVibrate(new long[0]) - .setContentIntent(pendingIntent); - - NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - - notificationManager.notify(requestCode, notificationBuilder.build()); - } + private static final String CHANNEL_ID = "permission"; + + public static void sendNotification(Context context, + String[] permissions, + int requestCode, + String notificationTitle, + String notificationText, + int notificationIcon, + ResultReceiver receiver) { + + Intent intent = new Intent(context, PermissionActivity.class); + intent.putExtra(Const.REQUEST_CODE, requestCode); + intent.putExtra(Const.PERMISSIONS_ARRAY, permissions); + intent.putExtra(Const.RESULT_RECEIVER, receiver); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); + } + + PendingIntent pendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_PUSH, intent, + PendingIntent.FLAG_ONE_SHOT); + + NotificationManager notificationManager = + (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, context.getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH); + notificationChannel.enableLights(true); + notificationChannel.setLightColor(Color.RED); + notificationChannel.setShowBadge(true); + notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + notificationManager.createNotificationChannel(notificationChannel); + } + + Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID) + .setSmallIcon(notificationIcon) + .setContentTitle(notificationTitle) + .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText)) + .setContentText(notificationText) + .setAutoCancel(true) + .setSound(defaultSoundUri) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setVibrate(new long[0]) + .setContentIntent(pendingIntent); + + notificationManager.notify(requestCode, notificationBuilder.build()); + } } diff --git a/library/src/main/res/values/strings.xml b/library/src/main/res/values/strings.xml index 566dea0..3e22db1 100644 --- a/library/src/main/res/values/strings.xml +++ b/library/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ PermissionEverywhere + Permission request