Skip to content

Commit

Permalink
Merge pull request #854 from lorenc-tomasz/Android_Change_Priority
Browse files Browse the repository at this point in the history
[Android] Allow to set notification's priority, visibility and importance options
  • Loading branch information
Gp2mv3 authored Sep 6, 2018
2 parents 3bd0b6f + 7b7e18b commit aaf2d19
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ PushNotification.localNotification({
tag: 'some_tag', // (optional) add tag to message
group: "group", // (optional) add group to message
ongoing: false, // (optional) set whether this is an "ongoing" notification
priority: "high", // (optional) set notification priority, default: high
visibility: "private", // (optional) set notification visibility, default: private
importance: "high", // (optional) set notification importance, default: high

/* iOS only properties */
alertAction: // (optional) default: view
Expand Down Expand Up @@ -302,6 +305,49 @@ PushNotification.localNotification({
PushNotification.cancelLocalNotifications({id: '123'});
```


## Notification priority ##

(optional) Specify `priority` to set priority of notification. Default value: "high"

Available options:

"max" = NotficationCompat.PRIORITY_MAX
"high" = NotficationCompat.PRIORITY_HIGH
"low" = NotficationCompat.PRIORITY_LOW
"min" = NotficationCompat.PRIORITY_MIN
"default" = NotficationCompat.PRIORITY_DEFAULT

More information: https://developer.android.com/reference/android/app/Notification.html#PRIORITY_DEFAULT

## Notification visibility ##

(optional) Specify `visibility` to set visibility of notification. Default value: "private"

Available options:

"private" = NotficationCompat.VISIBILITY_PRIVATE
"public" = NotficationCompat.VISIBILITY_PUBLIC
"secret" = NotficationCompat.VISIBILITY_SECRET

More information: https://developer.android.com/reference/android/app/Notification.html#VISIBILITY_PRIVATE

## Notification importance ##

(optional) Specify `importance` to set importance of notification. Default value: "high"

Available options:

"default" = NotificationManager.IMPORTANCE_DEFAULT
"max" = NotificationManager.IMPORTANCE_MAX
"high" = NotificationManager.IMPORTANCE_HIGH
"low" = NotificationManager.IMPORTANCE_LOW
"min" = NotificationManager.IMPORTANCE_MIN
"none" = NotificationManager.IMPORTANCE_NONE
"unspecified" = NotificationManager.IMPORTANCE_UNSPECIFIED

More information: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT

#### IOS
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.
```javascript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,55 @@ public void sendToNotificationCentre(Bundle bundle) {
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
}

int priority = NotificationCompat.PRIORITY_HIGH;
final String priorityString = bundle.getString("priority");

if (priorityString != null) {
switch(priorityString.toLowerCase()) {
case "max":
priority = NotificationCompat.PRIORITY_MAX;
break;
case "high":
priority = NotificationCompat.PRIORITY_HIGH;
break;
case "low":
priority = NotificationCompat.PRIORITY_LOW;
break;
case "min":
priority = NotificationCompat.PRIORITY_MIN;
break;
case "default":
priority = NotificationCompat.PRIORITY_DEFAULT;
break;
default:
priority = NotificationCompat.PRIORITY_HIGH;
}
}

int visibility = NotificationCompat.VISIBILITY_PRIVATE;
final String visibilityString = bundle.getString("visibility");

if (visibilityString != null) {
switch(visibilityString.toLowerCase()) {
case "private":
visibility = NotificationCompat.VISIBILITY_PRIVATE;
break;
case "public":
visibility = NotificationCompat.VISIBILITY_PUBLIC;
break;
case "secret":
visibility = NotificationCompat.VISIBILITY_SECRET;
break;
default:
visibility = NotificationCompat.VISIBILITY_PRIVATE;
}
}

NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentTitle(title)
.setTicker(bundle.getString("ticker"))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(visibility)
.setPriority(priority)
.setAutoCancel(bundle.getBoolean("autoCancel", true));

String group = bundle.getString("group");
Expand Down Expand Up @@ -492,7 +536,39 @@ private void checkOrCreateChannel(NotificationManager manager) {
if (manager == null)
return;

int importance = NotificationManager.IMPORTANCE_DEFAULT;
Bundle bundle = new Bundle();

int importance = NotificationManager.IMPORTANCE_HIGH;
final String importanceString = bundle.getString("importance");

if (importanceString != null) {
switch(importanceString.toLowerCase()) {
case "default":
importance = NotificationManager.IMPORTANCE_DEFAULT;
break;
case "max":
importance = NotificationManager.IMPORTANCE_MAX;
break;
case "high":
importance = NotificationManager.IMPORTANCE_HIGH;
break;
case "low":
importance = NotificationManager.IMPORTANCE_LOW;
break;
case "min":
importance = NotificationManager.IMPORTANCE_MIN;
break;
case "none":
importance = NotificationManager.IMPORTANCE_NONE;
break;
case "unspecified":
importance = NotificationManager.IMPORTANCE_UNSPECIFIED;
break;
default:
importance = NotificationManager.IMPORTANCE_HIGH;
}
}

NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.config.getChannelName(), importance);
channel.setDescription(this.config.getChannelDescription());
channel.enableLights(true);
Expand Down

0 comments on commit aaf2d19

Please sign in to comment.