Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support local notifications on Android O #657

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}