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

Add the notification channel for compatible with Android oreo #771

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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;
}
}