|
3 | 3 | import android.app.AlarmManager; |
4 | 4 | import android.app.Application; |
5 | 5 | import android.app.Notification; |
| 6 | +import android.app.NotificationChannel; |
6 | 7 | import android.app.NotificationManager; |
7 | 8 | import android.app.PendingIntent; |
8 | 9 | import android.content.Context; |
|
32 | 33 |
|
33 | 34 | public class ReactNativeNotificationsHandler extends NotificationsHandler { |
34 | 35 | public static final String TAG = "ReactNativeNotificationsHandler"; |
35 | | - |
| 36 | + private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id"; |
36 | 37 | private static final long DEFAULT_VIBRATION = 300L; |
37 | 38 |
|
38 | 39 | private Context context; |
@@ -102,7 +103,7 @@ private void sendNotification(Bundle bundle) { |
102 | 103 | title = context.getPackageManager().getApplicationLabel(appInfo).toString(); |
103 | 104 | } |
104 | 105 |
|
105 | | - NotificationCompat.Builder notification = new NotificationCompat.Builder(context) |
| 106 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) |
106 | 107 | .setContentTitle(title) |
107 | 108 | .setTicker(bundle.getString("ticker")) |
108 | 109 | .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) |
@@ -217,6 +218,7 @@ private void sendNotification(Bundle bundle) { |
217 | 218 | PendingIntent.FLAG_UPDATE_CURRENT); |
218 | 219 |
|
219 | 220 | NotificationManager notificationManager = notificationManager(); |
| 221 | + checkOrCreateChannel(notificationManager); |
220 | 222 |
|
221 | 223 | notification.setContentIntent(pendingIntent); |
222 | 224 |
|
@@ -276,4 +278,21 @@ private void sendNotification(Bundle bundle) { |
276 | 278 | private NotificationManager notificationManager() { |
277 | 279 | return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
278 | 280 | } |
| 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 | + } |
279 | 298 | } |
0 commit comments