Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 924fbbc

Browse files
authored
Merge pull request #657 from gcesarmza/master
Support local notifications on Android O
2 parents 5b34f15 + 8a090f0 commit 924fbbc

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

android/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def DEFAULT_SUPPORT_LIB_VERSION = "23.1.1"
2222
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "+"
2323

2424
android {
25-
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
26-
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
25+
compileSdkVersion 26
26+
buildToolsVersion "26.0.3"
2727

2828
defaultConfig {
2929
minSdkVersion 16
30-
targetSdkVersion project.hasProperty('targetSdkVersion') ? project.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
30+
targetSdkVersion 26
3131
versionCode 1
3232
versionName "1.0"
3333
ndk {
@@ -48,7 +48,7 @@ dependencies {
4848

4949
compile fileTree(dir: 'libs', include: ['*.jar'])
5050
testCompile 'junit:junit:4.12'
51-
compile "com.android.support:appcompat-v7:$supportLibVersion"
51+
compile 'com.android.support:appcompat-v7:26.0.+'
5252
compile 'com.facebook.react:react-native:+'
5353
compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
5454
compile 'me.leolin:ShortcutBadger:1.1.8@aar'

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.AlarmManager;
55
import android.app.Application;
66
import android.app.Notification;
7+
import android.app.NotificationChannel;
78
import android.app.NotificationManager;
89
import android.app.PendingIntent;
910
import android.content.Context;
@@ -34,6 +35,7 @@
3435
public class RNPushNotificationHelper {
3536
public static final String PREFERENCES_KEY = "rn_push_notification";
3637
private static final long DEFAULT_VIBRATION = 300L;
38+
private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3739

3840
private Context context;
3941
private final SharedPreferences scheduledNotificationsPersistence;
@@ -157,7 +159,7 @@ public void sendToNotificationCentre(Bundle bundle) {
157159
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
158160
}
159161

160-
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
162+
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
161163
.setContentTitle(title)
162164
.setTicker(bundle.getString("ticker"))
163165
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
@@ -272,6 +274,7 @@ public void sendToNotificationCentre(Bundle bundle) {
272274
PendingIntent.FLAG_UPDATE_CURRENT);
273275

274276
NotificationManager notificationManager = notificationManager();
277+
checkOrCreateChannel(notificationManager);
275278

276279
notification.setContentIntent(pendingIntent);
277280

@@ -464,4 +467,23 @@ private static void commit(SharedPreferences.Editor editor) {
464467
editor.apply();
465468
}
466469
}
470+
471+
private static boolean channelCreated = false;
472+
private static void checkOrCreateChannel(NotificationManager manager) {
473+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
474+
return;
475+
if (channelCreated)
476+
return;
477+
if (manager == null)
478+
return;
479+
480+
final CharSequence name = "rn-push-notification-channel";
481+
int importance = NotificationManager.IMPORTANCE_DEFAULT;
482+
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
483+
channel.enableLights(true);
484+
channel.enableVibration(true);
485+
486+
manager.createNotificationChannel(channel);
487+
channelCreated = true;
488+
}
467489
}

0 commit comments

Comments
 (0)