Skip to content

Commit

Permalink
Merge pull request #657 from gcesarmza/master
Browse files Browse the repository at this point in the history
Support local notifications on Android O
  • Loading branch information
Gp2mv3 committed Jul 4, 2018
2 parents 5b34f15 + 8a090f0 commit 924fbbc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def DEFAULT_SUPPORT_LIB_VERSION = "23.1.1"
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "+"

android {
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
compileSdkVersion 26
buildToolsVersion "26.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion project.hasProperty('targetSdkVersion') ? project.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
targetSdkVersion 26
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -48,7 +48,7 @@ dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile 'com.android.support:appcompat-v7:26.0.+'
compile 'com.facebook.react:react-native:+'
compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
compile 'me.leolin:ShortcutBadger:1.1.8@aar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -272,6 +274,7 @@ public void sendToNotificationCentre(Bundle bundle) {
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager notificationManager = notificationManager();
checkOrCreateChannel(notificationManager);

notification.setContentIntent(pendingIntent);

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 924fbbc

Please sign in to comment.