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

Commit 7b5f945

Browse files
committed
Allow to define a default channel in AndroidManifest, useful for foreground notification.
1 parent a9e3ab8 commit 7b5f945

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
1212

1313
### Fixed
1414

15+
## [6.1.0] 2020-09-28
16+
17+
### Features
18+
19+
- (Android) Allow a default channel in the `AndroidManifest`:
20+
```xml
21+
<meta-data android:name="com.dieam.reactnativepushnotification.default_notification_channel_id" android:value="..."/>
22+
```
23+
If not defined, fallback to the Firebase value of:
24+
```xml
25+
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="..."/>
26+
```
27+
If not defined, fallback to the default Firebase channel id `fcm_fallback_notification_channel`
28+
1529
## [6.0.0] 2020-09-26
1630

1731
### Breaking changes

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,22 @@ If you want to use a different default channel for remote notification, refer to
420420
android:value="@string/default_notification_channel_id" />
421421
```
422422

423+
For local notifications, the same kind of option is available:
424+
425+
- you can use:
426+
```xml
427+
<meta-data
428+
android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
429+
android:value="@string/default_notification_channel_id" />
430+
```
431+
- If not defined, fallback to the Firebase value defined in the `AndroidManifest`:
432+
```xml
433+
<meta-data
434+
android:name="com.google.firebase.messaging.default_notification_channel_id"
435+
android:value="..." />
436+
```
437+
- If not defined, fallback to the default Firebase channel id `fcm_fallback_notification_channel`
438+
423439
### List channels
424440

425441
You can list available channels with:

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.util.Log;
99

1010
class RNPushNotificationConfig {
11+
private static final String KEY_NOTIFICATION_FIREBASE_DEFAULT_CHANNEL_ID = "com.google.firebase.messaging.default_notification_channel_id";
12+
private static final String KEY_NOTIFICATION_DEFAULT_CHANNEL_ID = "com.dieam.reactnativepushnotification.default_notification_channel_id";
1113
private static final String KEY_NOTIFICATION_FOREGROUND = "com.dieam.reactnativepushnotification.notification_foreground";
1214
private static final String KEY_NOTIFICATION_COLOR = "com.dieam.reactnativepushnotification.notification_color";
1315

@@ -63,4 +65,16 @@ public boolean getNotificationForeground() {
6365
// Default
6466
return false;
6567
}
68+
69+
public String getNotificationDefaultChannelId() {
70+
try {
71+
return getStringValue(KEY_NOTIFICATION_DEFAULT_CHANNEL_ID,
72+
getStringValue(KEY_NOTIFICATION_FIREBASE_DEFAULT_CHANNEL_ID, "fcm_fallback_notification_channel")
73+
);
74+
} catch (Exception e) {
75+
Log.w(RNPushNotification.LOG_TAG, "Unable to find " + KEY_NOTIFICATION_DEFAULT_CHANNEL_ID + " in manifest. Falling back to default");
76+
}
77+
// Default
78+
return "fcm_fallback_notification_channel";
79+
}
6680
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
269269
}
270270

271271
String channel_id = bundle.getString("channelId");
272+
273+
if(channel_id == null) {
274+
channel_id = this.config.getNotificationDefaultChannelId();
275+
}
272276

273277
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channel_id)
274278
.setContentTitle(title)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131

3232
public class RNReceivedMessageHandler {
3333
private FirebaseMessagingService mFirebaseMessagingService;
34+
private RNPushNotificationConfig config;
3435

3536
public RNReceivedMessageHandler(@NonNull FirebaseMessagingService service) {
3637
this.mFirebaseMessagingService = service;
38+
this.config = new RNPushNotificationConfig(service.getApplication());
3739
}
3840

3941
public void handleReceivedMessage(RemoteMessage message) {
@@ -54,7 +56,7 @@ public void handleReceivedMessage(RemoteMessage message) {
5456
bundle.putString("channelId", remoteNotification.getChannelId());
5557
}
5658
else {
57-
bundle.putString("channelId", "fcm_fallback_notification_channel");
59+
bundle.putString("channelId", this.config.getNotificationDefaultChannelId());
5860
}
5961

6062
Integer visibilty = remoteNotification.getVisibility();
@@ -155,7 +157,6 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
155157

156158
Application applicationContext = (Application) context.getApplicationContext();
157159

158-
RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());
159160
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
160161

161162
boolean isForeground = pushNotificationHelper.isApplicationInForeground();
@@ -170,7 +171,7 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
170171
jsDelivery.notifyRemoteFetch(bundle);
171172
}
172173

173-
if (config.getNotificationForeground() || !isForeground) {
174+
if (this.config.getNotificationForeground() || !isForeground) {
174175
Log.v(LOG_TAG, "sendNotification: " + bundle);
175176

176177
pushNotificationHelper.sendToNotificationCentre(bundle);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-push-notification",
3-
"version": "6.0.0",
3+
"version": "6.1.0",
44
"description": "React Native Local and Remote Notifications",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)