From acfd0792d7ccc70d9818eee8398d995704320be9 Mon Sep 17 00:00:00 2001 From: Gustavo Genovese Date: Thu, 22 Feb 2018 14:17:13 -0300 Subject: [PATCH] Support local notifications on Android O --- android/build.gradle | 8 +++---- .../modules/RNPushNotificationHelper.java | 24 ++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 7937d10c2..c92d0f553 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -16,12 +16,12 @@ allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 26 + buildToolsVersion "26.0.3" defaultConfig { minSdkVersion 16 - targetSdkVersion 23 + targetSdkVersion 26 versionCode 1 versionName "1.0" ndk { @@ -39,7 +39,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.android.support:appcompat-v7:26.0.+' compile 'com.facebook.react:react-native:+' compile 'com.google.android.gms:play-services-gcm:+' compile 'me.leolin:ShortcutBadger:1.1.8@aar' diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 988cccf49..523b766d3 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -4,6 +4,7 @@ import android.app.AlarmManager; import android.app.Application; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -34,6 +35,7 @@ public class RNPushNotificationHelper { public static final String PREFERENCES_KEY = "rn_push_notification"; private static final long DEFAULT_VIBRATION = 300L; + private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id"; private Context context; private final SharedPreferences scheduledNotificationsPersistence; @@ -157,7 +159,7 @@ public void sendToNotificationCentre(Bundle bundle) { title = context.getPackageManager().getApplicationLabel(appInfo).toString(); } - NotificationCompat.Builder notification = new NotificationCompat.Builder(context) + NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) .setContentTitle(title) .setTicker(bundle.getString("ticker")) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) @@ -272,6 +274,7 @@ public void sendToNotificationCentre(Bundle bundle) { PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = notificationManager(); + checkOrCreateChannel(notificationManager); notification.setContentIntent(pendingIntent); @@ -464,4 +467,23 @@ private static void commit(SharedPreferences.Editor editor) { editor.apply(); } } + + private static boolean channelCreated = false; + private static void checkOrCreateChannel(NotificationManager manager) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + return; + if (channelCreated) + return; + if (manager == null) + return; + + final CharSequence name = "rn-push-notification-channel"; + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance); + channel.enableLights(true); + channel.enableVibration(true); + + manager.createNotificationChannel(channel); + channelCreated = true; + } }