Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 7516032

Browse files
committed
Integrate change from #1042.
Close #1042
1 parent c118ac1 commit 7516032

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2020
- Fix: security issue `limit the components that Intent will resolve to` [#687](https://github.com/zo0r/react-native-push-notification/pull/687)
2121
- Fix: remove fishy reference from android project files [#1226](https://github.com/zo0r/react-native-push-notification/pull/1226)
2222
- Fix: `JSON value '<null>' of type NSNull cannot be converted to NSDictionary` [#1030](https://github.com/zo0r/react-native-push-notification/pull/1030)
23+
- Fix: Fixed foreground FCM banner notifications and notification sound [#1042](https://github.com/zo0r/react-native-push-notification/pull/1042)
2324
- Upgrade ShortCutBadger to 1.1.22 [#646](https://github.com/zo0r/react-native-push-notification/pull/646)
2425
- Upgrade exemple to React-Native 0.62.2
2526
- Remove Types from the code use [@types/react-native-push-notification](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-native-push-notification) instead.

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

+48-14
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,22 @@ public void sendToNotificationCentre(Bundle bundle) {
216216
}
217217
}
218218

219-
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
219+
Strint channel_id = NOTIFICATION_CHANNEL_ID;
220+
221+
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channel_id)
220222
.setContentTitle(title)
221223
.setTicker(bundle.getString("ticker"))
222224
.setVisibility(visibility)
223225
.setPriority(priority)
224226
.setAutoCancel(bundle.getBoolean("autoCancel", true));
225-
227+
228+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26 and higher
229+
// Changing Default mode of notification
230+
notification.setDefaults(Notification.DEFAULT_LIGHTS);
231+
}
232+
226233
String group = bundle.getString("group");
234+
227235
if (group != null) {
228236
notification.setGroup(group);
229237
}
@@ -288,9 +296,13 @@ public void sendToNotificationCentre(Bundle bundle) {
288296
bundle.putBoolean("userInteraction", true);
289297
intent.putExtra("notification", bundle);
290298

299+
Uri soundUri = null;
300+
291301
if (!bundle.containsKey("playSound") || bundle.getBoolean("playSound")) {
292-
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
302+
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
303+
293304
String soundName = bundle.getString("soundName");
305+
294306
if (soundName != null) {
295307
if (!"default".equalsIgnoreCase(soundName)) {
296308

@@ -307,11 +319,22 @@ public void sendToNotificationCentre(Bundle bundle) {
307319
}
308320

309321
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resId);
322+
323+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26 and higher
324+
channel_id = channel_id + "-" + soundName;
325+
326+
notification.setChannelId(channel_id);
327+
}
310328
}
311329
}
330+
312331
notification.setSound(soundUri);
313332
}
314333

334+
if (soundUri == null || Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
335+
notification.setSound(null);
336+
}
337+
315338
if (bundle.containsKey("ongoing") || bundle.getBoolean("ongoing")) {
316339
notification.setOngoing(bundle.getBoolean("ongoing"));
317340
}
@@ -334,7 +357,8 @@ public void sendToNotificationCentre(Bundle bundle) {
334357
PendingIntent.FLAG_UPDATE_CURRENT);
335358

336359
NotificationManager notificationManager = notificationManager();
337-
checkOrCreateChannel(notificationManager);
360+
361+
checkOrCreateChannel(notificationManager, channel_id, soundUri);
338362

339363
notification.setContentIntent(pendingIntent);
340364

@@ -561,12 +585,9 @@ private static void commit(SharedPreferences.Editor editor) {
561585
}
562586
}
563587

564-
private static boolean channelCreated = false;
565-
private void checkOrCreateChannel(NotificationManager manager) {
588+
private void checkOrCreateChannel(NotificationManager manager, String channel_id, Uri soundUri) {
566589
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
567590
return;
568-
if (channelCreated)
569-
return;
570591
if (manager == null)
571592
return;
572593

@@ -603,14 +624,27 @@ private void checkOrCreateChannel(NotificationManager manager) {
603624
}
604625
}
605626

606-
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.config.getChannelName() != null ? this.config.getChannelName() : "rn-push-notification-channel", importance);
627+
NotificationChannel channel = manager.getNotificationChannel(channel_id);
607628

608-
channel.setDescription(this.config.getChannelDescription());
609-
channel.enableLights(true);
610-
channel.enableVibration(true);
629+
if (channel == null) {
630+
channel = new NotificationChannel(channel_id, this.config.getChannelName() != null ? this.config.getChannelName() : "rn-push-notification-channel", importance);
631+
channel.enableLights(true);
632+
channel.enableVibration(true);
633+
channel.setDescription(this.config.getChannelDescription());
611634

612-
manager.createNotificationChannel(channel);
613-
channelCreated = true;
635+
if (soundUri != null) {
636+
AudioAttributes audioAttributes = new AudioAttributes.Builder()
637+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
638+
.setUsage(AudioAttributes.USAGE_ALARM)
639+
.build();
640+
641+
channel.setSound(soundUri, audioAttributes);
642+
} else {
643+
channel.setSound(null, null);
644+
}
645+
646+
manager.createNotificationChannel(channel);
647+
}
614648
}
615649

616650
private boolean isApplicationInForeground(Context context) {

0 commit comments

Comments
 (0)