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

Commit aaf2d19

Browse files
authored
Merge pull request #854 from lorenc-tomasz/Android_Change_Priority
[Android] Allow to set notification's priority, visibility and importance options
2 parents 3bd0b6f + 7b7e18b commit aaf2d19

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ PushNotification.localNotification({
246246
tag: 'some_tag', // (optional) add tag to message
247247
group: "group", // (optional) add group to message
248248
ongoing: false, // (optional) set whether this is an "ongoing" notification
249+
priority: "high", // (optional) set notification priority, default: high
250+
visibility: "private", // (optional) set notification visibility, default: private
251+
importance: "high", // (optional) set notification importance, default: high
249252

250253
/* iOS only properties */
251254
alertAction: // (optional) default: view
@@ -302,6 +305,49 @@ PushNotification.localNotification({
302305
PushNotification.cancelLocalNotifications({id: '123'});
303306
```
304307

308+
309+
## Notification priority ##
310+
311+
(optional) Specify `priority` to set priority of notification. Default value: "high"
312+
313+
Available options:
314+
315+
"max" = NotficationCompat.PRIORITY_MAX
316+
"high" = NotficationCompat.PRIORITY_HIGH
317+
"low" = NotficationCompat.PRIORITY_LOW
318+
"min" = NotficationCompat.PRIORITY_MIN
319+
"default" = NotficationCompat.PRIORITY_DEFAULT
320+
321+
More information: https://developer.android.com/reference/android/app/Notification.html#PRIORITY_DEFAULT
322+
323+
## Notification visibility ##
324+
325+
(optional) Specify `visibility` to set visibility of notification. Default value: "private"
326+
327+
Available options:
328+
329+
"private" = NotficationCompat.VISIBILITY_PRIVATE
330+
"public" = NotficationCompat.VISIBILITY_PUBLIC
331+
"secret" = NotficationCompat.VISIBILITY_SECRET
332+
333+
More information: https://developer.android.com/reference/android/app/Notification.html#VISIBILITY_PRIVATE
334+
335+
## Notification importance ##
336+
337+
(optional) Specify `importance` to set importance of notification. Default value: "high"
338+
339+
Available options:
340+
341+
"default" = NotificationManager.IMPORTANCE_DEFAULT
342+
"max" = NotificationManager.IMPORTANCE_MAX
343+
"high" = NotificationManager.IMPORTANCE_HIGH
344+
"low" = NotificationManager.IMPORTANCE_LOW
345+
"min" = NotificationManager.IMPORTANCE_MIN
346+
"none" = NotificationManager.IMPORTANCE_NONE
347+
"unspecified" = NotificationManager.IMPORTANCE_UNSPECIFIED
348+
349+
More information: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT
350+
305351
#### IOS
306352
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.
307353
```javascript

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

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,55 @@ public void sendToNotificationCentre(Bundle bundle) {
161161
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
162162
}
163163

164+
int priority = NotificationCompat.PRIORITY_HIGH;
165+
final String priorityString = bundle.getString("priority");
166+
167+
if (priorityString != null) {
168+
switch(priorityString.toLowerCase()) {
169+
case "max":
170+
priority = NotificationCompat.PRIORITY_MAX;
171+
break;
172+
case "high":
173+
priority = NotificationCompat.PRIORITY_HIGH;
174+
break;
175+
case "low":
176+
priority = NotificationCompat.PRIORITY_LOW;
177+
break;
178+
case "min":
179+
priority = NotificationCompat.PRIORITY_MIN;
180+
break;
181+
case "default":
182+
priority = NotificationCompat.PRIORITY_DEFAULT;
183+
break;
184+
default:
185+
priority = NotificationCompat.PRIORITY_HIGH;
186+
}
187+
}
188+
189+
int visibility = NotificationCompat.VISIBILITY_PRIVATE;
190+
final String visibilityString = bundle.getString("visibility");
191+
192+
if (visibilityString != null) {
193+
switch(visibilityString.toLowerCase()) {
194+
case "private":
195+
visibility = NotificationCompat.VISIBILITY_PRIVATE;
196+
break;
197+
case "public":
198+
visibility = NotificationCompat.VISIBILITY_PUBLIC;
199+
break;
200+
case "secret":
201+
visibility = NotificationCompat.VISIBILITY_SECRET;
202+
break;
203+
default:
204+
visibility = NotificationCompat.VISIBILITY_PRIVATE;
205+
}
206+
}
207+
164208
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
165209
.setContentTitle(title)
166210
.setTicker(bundle.getString("ticker"))
167-
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
168-
.setPriority(NotificationCompat.PRIORITY_HIGH)
211+
.setVisibility(visibility)
212+
.setPriority(priority)
169213
.setAutoCancel(bundle.getBoolean("autoCancel", true));
170214

171215
String group = bundle.getString("group");
@@ -492,7 +536,39 @@ private void checkOrCreateChannel(NotificationManager manager) {
492536
if (manager == null)
493537
return;
494538

495-
int importance = NotificationManager.IMPORTANCE_DEFAULT;
539+
Bundle bundle = new Bundle();
540+
541+
int importance = NotificationManager.IMPORTANCE_HIGH;
542+
final String importanceString = bundle.getString("importance");
543+
544+
if (importanceString != null) {
545+
switch(importanceString.toLowerCase()) {
546+
case "default":
547+
importance = NotificationManager.IMPORTANCE_DEFAULT;
548+
break;
549+
case "max":
550+
importance = NotificationManager.IMPORTANCE_MAX;
551+
break;
552+
case "high":
553+
importance = NotificationManager.IMPORTANCE_HIGH;
554+
break;
555+
case "low":
556+
importance = NotificationManager.IMPORTANCE_LOW;
557+
break;
558+
case "min":
559+
importance = NotificationManager.IMPORTANCE_MIN;
560+
break;
561+
case "none":
562+
importance = NotificationManager.IMPORTANCE_NONE;
563+
break;
564+
case "unspecified":
565+
importance = NotificationManager.IMPORTANCE_UNSPECIFIED;
566+
break;
567+
default:
568+
importance = NotificationManager.IMPORTANCE_HIGH;
569+
}
570+
}
571+
496572
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.config.getChannelName(), importance);
497573
channel.setDescription(this.config.getChannelDescription());
498574
channel.enableLights(true);

0 commit comments

Comments
 (0)