Skip to content

Commit ac54b96

Browse files
authored
Merge pull request #49 from Eugnis/master
Support for RN 0.56 and new Google Play rules (target SDK 26)
2 parents b0e5ca2 + 37edf6f commit ac54b96

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

android/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.3"
66

77
defaultConfig {
88
minSdkVersion 16
9-
targetSdkVersion 23
9+
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"
1212
ndk {
@@ -26,6 +26,7 @@ dependencies {
2626
compile 'com.facebook.react:react-native:+'
2727
compile 'com.google.android.gms:play-services-gcm:9.4.0'
2828
compile 'com.google.firebase:firebase-messaging:9.4.0'
29+
compile 'com.android.support:appcompat-v7:26.0.+'
2930
compile 'com.microsoft.azure:notification-hubs-android-sdk:0.4@aar'
3031
compile 'com.microsoft.azure:azure-notifications-handler:1.0.1@aar'
3132
}

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationsHandler.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.AlarmManager;
44
import android.app.Application;
55
import android.app.Notification;
6+
import android.app.NotificationChannel;
67
import android.app.NotificationManager;
78
import android.app.PendingIntent;
89
import android.content.Context;
@@ -32,7 +33,7 @@
3233

3334
public class ReactNativeNotificationsHandler extends NotificationsHandler {
3435
public static final String TAG = "ReactNativeNotificationsHandler";
35-
36+
private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3637
private static final long DEFAULT_VIBRATION = 300L;
3738

3839
private Context context;
@@ -102,7 +103,7 @@ private void sendNotification(Bundle bundle) {
102103
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
103104
}
104105

105-
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
106+
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
106107
.setContentTitle(title)
107108
.setTicker(bundle.getString("ticker"))
108109
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
@@ -217,6 +218,7 @@ private void sendNotification(Bundle bundle) {
217218
PendingIntent.FLAG_UPDATE_CURRENT);
218219

219220
NotificationManager notificationManager = notificationManager();
221+
checkOrCreateChannel(notificationManager);
220222

221223
notification.setContentIntent(pendingIntent);
222224

@@ -276,4 +278,21 @@ private void sendNotification(Bundle bundle) {
276278
private NotificationManager notificationManager() {
277279
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
278280
}
281+
282+
private static boolean channelCreated = false;
283+
private static void checkOrCreateChannel(NotificationManager manager) {
284+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
285+
return;
286+
if (channelCreated)
287+
return;
288+
if (manager == null)
289+
return;
290+
final CharSequence name = "rn-push-notification-channel";
291+
int importance = NotificationManager.IMPORTANCE_DEFAULT;
292+
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
293+
channel.enableLights(true);
294+
channel.enableVibration(true);
295+
manager.createNotificationChannel(channel);
296+
channelCreated = true;
297+
}
279298
}

0 commit comments

Comments
 (0)