Skip to content

Commit af92543

Browse files
authored
Support notifications on Android 8+
1 parent b0e5ca2 commit af92543

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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)