|
4 | 4 | import android.app.AlarmManager;
|
5 | 5 | import android.app.Application;
|
6 | 6 | import android.app.Notification;
|
| 7 | +import android.app.NotificationChannel; |
7 | 8 | import android.app.NotificationManager;
|
8 | 9 | import android.app.PendingIntent;
|
9 | 10 | import android.content.Context;
|
|
34 | 35 | public class RNPushNotificationHelper {
|
35 | 36 | public static final String PREFERENCES_KEY = "rn_push_notification";
|
36 | 37 | private static final long DEFAULT_VIBRATION = 300L;
|
| 38 | + private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id"; |
37 | 39 |
|
38 | 40 | private Context context;
|
39 | 41 | private final SharedPreferences scheduledNotificationsPersistence;
|
@@ -157,7 +159,7 @@ public void sendToNotificationCentre(Bundle bundle) {
|
157 | 159 | title = context.getPackageManager().getApplicationLabel(appInfo).toString();
|
158 | 160 | }
|
159 | 161 |
|
160 |
| - NotificationCompat.Builder notification = new NotificationCompat.Builder(context) |
| 162 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) |
161 | 163 | .setContentTitle(title)
|
162 | 164 | .setTicker(bundle.getString("ticker"))
|
163 | 165 | .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
@@ -272,6 +274,7 @@ public void sendToNotificationCentre(Bundle bundle) {
|
272 | 274 | PendingIntent.FLAG_UPDATE_CURRENT);
|
273 | 275 |
|
274 | 276 | NotificationManager notificationManager = notificationManager();
|
| 277 | + checkOrCreateChannel(notificationManager); |
275 | 278 |
|
276 | 279 | notification.setContentIntent(pendingIntent);
|
277 | 280 |
|
@@ -464,4 +467,23 @@ private static void commit(SharedPreferences.Editor editor) {
|
464 | 467 | editor.apply();
|
465 | 468 | }
|
466 | 469 | }
|
| 470 | + |
| 471 | + private static boolean channelCreated = false; |
| 472 | + private static void checkOrCreateChannel(NotificationManager manager) { |
| 473 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) |
| 474 | + return; |
| 475 | + if (channelCreated) |
| 476 | + return; |
| 477 | + if (manager == null) |
| 478 | + return; |
| 479 | + |
| 480 | + final CharSequence name = "rn-push-notification-channel"; |
| 481 | + int importance = NotificationManager.IMPORTANCE_DEFAULT; |
| 482 | + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance); |
| 483 | + channel.enableLights(true); |
| 484 | + channel.enableVibration(true); |
| 485 | + |
| 486 | + manager.createNotificationChannel(channel); |
| 487 | + channelCreated = true; |
| 488 | + } |
467 | 489 | }
|
0 commit comments