Skip to content

Commit f7b4759

Browse files
authored
Merge pull request zo0r#822 from Truebill/channel-config-rev2
Allow configuring notification channel and color through manifest
2 parents 8718e61 + 51f55e7 commit f7b4759

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ In your `AndroidManifest.xml`
7070
<uses-permission android:name="android.permission.VIBRATE" />
7171
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
7272

73+
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
74+
android:value="YOUR NOTIFICATION CHANNEL NAME"/>
75+
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description"
76+
android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
77+
<!-- Change the resource name to your App's accent color - or any other color you want -->
78+
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
79+
android:resource="@android:color/white"/>
80+
7381
<application ....>
7482
<!-- < Only if you're using GCM or localNotificationSchedule() > -->
7583
<receiver
@@ -272,7 +280,7 @@ In the location notification json specify the full file name:
272280
The `id` parameter for `PushNotification.localNotification` is required for this operation. The id supplied will then be used for the cancel operation.
273281

274282
```javascript
275-
// Android
283+
// Android
276284
PushNotification.localNotification({
277285
...
278286
id: '123'
@@ -284,7 +292,7 @@ PushNotification.cancelLocalNotifications({id: '123'});
284292
#### IOS
285293
The `userInfo` parameter for `PushNotification.localNotification` is required for this operation and must contain an `id` parameter. The id supplied will then be used for the cancel operation.
286294
```javascript
287-
// IOS
295+
// IOS
288296
PushNotification.localNotification({
289297
...
290298
userInfo: { id: '123' }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public RNPushNotification(ReactApplicationContext reactContext) {
4343
reactContext.addActivityEventListener(this);
4444

4545
Application applicationContext = (Application) reactContext.getApplicationContext();
46+
4647
// The @ReactNative methods use this
4748
mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext);
4849
// This is used to delivery callbacks to JS
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.dieam.reactnativepushnotification.modules;
2+
3+
import android.content.Context;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.PackageManager;
6+
import android.support.v4.content.res.ResourcesCompat;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
10+
class RNPushNotificationConfig {
11+
private static final String KEY_CHANNEL_NAME = "com.dieam.reactnativepushnotification.notification_channel_name";
12+
private static final String KEY_CHANNEL_DESCRIPTION = "com.dieam.reactnativepushnotification.notification_channel_description";
13+
private static final String KEY_NOTIFICATION_COLOR = "com.dieam.reactnativepushnotification.notification_color";
14+
15+
private static Bundle metadata;
16+
private Context context;
17+
18+
public RNPushNotificationConfig(Context context) {
19+
this.context = context;
20+
if (metadata == null) {
21+
try {
22+
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
23+
metadata = applicationInfo.metaData;
24+
} catch (PackageManager.NameNotFoundException e) {
25+
e.printStackTrace();
26+
Log.e(RNPushNotification.LOG_TAG, "Error reading application meta, falling back to defaults");
27+
metadata = new Bundle();
28+
}
29+
}
30+
}
31+
32+
public String getChannelName() {
33+
try {
34+
return metadata.getString(KEY_CHANNEL_NAME);
35+
} catch (Exception e) {
36+
Log.w(RNPushNotification.LOG_TAG, "Unable to find " + KEY_CHANNEL_NAME + " in manifest. Falling back to default");
37+
}
38+
// Default
39+
return "rn-push-notification-channel";
40+
}
41+
public String getChannelDescription() {
42+
try {
43+
return metadata.getString(KEY_CHANNEL_DESCRIPTION);
44+
} catch (Exception e) {
45+
Log.w(RNPushNotification.LOG_TAG, "Unable to find " + KEY_CHANNEL_DESCRIPTION + " in manifest. Falling back to default");
46+
}
47+
// Default
48+
return "";
49+
}
50+
public int getNotificationColor() {
51+
try {
52+
int resourceId = metadata.getInt(KEY_NOTIFICATION_COLOR);
53+
return ResourcesCompat.getColor(context.getResources(), resourceId, null);
54+
} catch (Exception e) {
55+
Log.w(RNPushNotification.LOG_TAG, "Unable to find " + KEY_NOTIFICATION_COLOR + " in manifest. Falling back to default");
56+
}
57+
// Default
58+
return -1;
59+
}
60+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ public class RNPushNotificationHelper {
3838
private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3939

4040
private Context context;
41+
private RNPushNotificationConfig config;
4142
private final SharedPreferences scheduledNotificationsPersistence;
4243
private static final int ONE_MINUTE = 60 * 1000;
4344
private static final long ONE_HOUR = 60 * ONE_MINUTE;
4445
private static final long ONE_DAY = 24 * ONE_HOUR;
4546

4647
public RNPushNotificationHelper(Application context) {
4748
this.context = context;
49+
this.config = new RNPushNotificationConfig(context);
4850
this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
4951
}
5052

@@ -263,8 +265,11 @@ public void sendToNotificationCentre(Bundle bundle) {
263265
notification.setCategory(NotificationCompat.CATEGORY_CALL);
264266

265267
String color = bundle.getString("color");
268+
int defaultColor = this.config.getNotificationColor();
266269
if (color != null) {
267270
notification.setColor(Color.parseColor(color));
271+
} else if (defaultColor != -1) {
272+
notification.setColor(defaultColor);
268273
}
269274
}
270275

@@ -476,17 +481,17 @@ private static void commit(SharedPreferences.Editor editor) {
476481
}
477482

478483
private static boolean channelCreated = false;
479-
private static void checkOrCreateChannel(NotificationManager manager) {
484+
private void checkOrCreateChannel(NotificationManager manager) {
480485
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
481486
return;
482487
if (channelCreated)
483488
return;
484489
if (manager == null)
485490
return;
486491

487-
final CharSequence name = "rn-push-notification-channel";
488492
int importance = NotificationManager.IMPORTANCE_DEFAULT;
489-
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
493+
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.config.getChannelName(), importance);
494+
channel.setDescription(this.config.getChannelDescription());
490495
channel.enableLights(true);
491496
channel.enableVibration(true);
492497

0 commit comments

Comments
 (0)