Skip to content

Commit

Permalink
Merge pull request #1959 from Mookiies/addConstants
Browse files Browse the repository at this point in the history
Add constants for notification importance
  • Loading branch information
Boris Tacyniak authored Apr 23, 2021
2 parents 4110df8 + 89303a0 commit 70c43d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
72 changes: 33 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,6 @@ Having a problem? Read the [troubleshooting](./trouble-shooting.md) guide before

The component uses PushNotificationIOS for the iOS part. You should follow their [installation instructions](https://github.com/react-native-community/react-native-push-notification-ios).

When done, modify the following method in the file `AppDelegate.m`:
```objective-c
// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
// Still call the JS onNotification handler so it can display the new message right away
NSDictionary *userInfo = notification.request.content.userInfo;
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo
fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];

// allow showing foreground notifications
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
// or if you wish to hide all notification while in foreground replace it with
// completionHandler(UNNotificationPresentationOptionNone);
}
```

## Android manual Installation

**NOTE: `firebase-messaging`, prior to version 15 requires to have the same version number in order to work correctly at build time and at run time. To use a specific version:**
Expand Down Expand Up @@ -142,6 +123,17 @@ In `android/app/src/main/res/values/colors.xml` (Create the file if it doesn't e
</resources>
```

If your app has an @Override on onNewIntent in `MainActivity.java` ensure that function includes a super call on onNewIntent (if your `MainActivity.java` does not have an @Override for onNewIntent skip this):

```java
@Override
public void onNewIntent(Intent intent) {
...
super.onNewIntent(intent);
...
}
```

### If you use remote notifications

Make sure you have installed setup Firebase correctly.
Expand Down Expand Up @@ -217,10 +209,10 @@ public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {

return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage() // <---- Add the Package
);
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativePushNotificationPackage() // <---- Add the Package
);
}
};

Expand Down Expand Up @@ -411,14 +403,16 @@ In the location notification json specify the full file name:
To use channels, create them at startup and pass the matching `channelId` through to `PushNotification.localNotification` or `PushNotification.localNotificationSchedule`.

```javascript
import PushNotification, {Importance} from 'react-native-push-notification';
...
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
playSound: false, // (optional) default: true
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
Expand Down Expand Up @@ -598,10 +592,10 @@ Returns an array of local scheduled notification objects containing:

Available options:

"max" = NotficationCompat.PRIORITY_MAX
"high" = NotficationCompat.PRIORITY_HIGH
"low" = NotficationCompat.PRIORITY_LOW
"min" = NotficationCompat.PRIORITY_MIN
"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
Expand All @@ -612,25 +606,25 @@ More information: https://developer.android.com/reference/android/app/Notificati

Available options:

"private" = NotficationCompat.VISIBILITY_PRIVATE
"public" = NotficationCompat.VISIBILITY_PUBLIC
"secret" = NotficationCompat.VISIBILITY_SECRET
"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"
(optional) Specify `importance` to set importance of notification. Default value: Importance.HIGH
Constants available on the `Importance` object. `import PushNotification, {Importance} from 'react-native-push-notification';`

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
Importance.DEFAULT = NotificationManager.IMPORTANCE_DEFAULT\
Importance.HIGH = NotificationManager.IMPORTANCE_HIGH\
Importance.LOW = NotificationManager.IMPORTANCE_LOW\
Importance.MIN = NotificationManager.IMPORTANCE_MIN\
Importance.NONE= NotificationManager.IMPORTANCE_NONE\
Importance.UNSPECIFIED = NotificationManager.IMPORTANCE_UNSPECIFIED

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

Expand Down
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
Expand All @@ -10,6 +11,7 @@ buildscript {

allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
Expand Down
8 changes: 4 additions & 4 deletions example/NotifService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PushNotification from 'react-native-push-notification';
import PushNotification, {Importance} from 'react-native-push-notification';
import NotificationHandler from './NotificationHandler';

export default class NotifService {
Expand Down Expand Up @@ -30,7 +30,7 @@ export default class NotifService {
channelName: `Default channel`, // (required)
channelDescription: "A default channel", // (optional) default: undefined.
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel 'default-channel-id' returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
Expand All @@ -41,7 +41,7 @@ export default class NotifService {
channelName: `Sound channel`, // (required)
channelDescription: "A sound channel", // (optional) default: undefined.
soundName: "sample.mp3", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel 'sound-channel-id' returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
Expand All @@ -56,7 +56,7 @@ export default class NotifService {
channelName: `Custom channel - Counter: ${this.lastChannelCounter}`, // (required)
channelDescription: `A custom channel to categorise your custom notifications. Updated at: ${Date.now()}`, // (optional) default: undefined.
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: 4, // (optional) default: 4. Int value of the Android notification importance
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,14 @@ Notifications.setNotificationCategories = function() {
return this.callNative('setNotificationCategories', arguments);
}

// https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT
Notifications.Importance = Object.freeze({
DEFAULT: 3,
HIGH: 4,
LOW: 2,
MIN: 1,
NONE: 0,
UNSPECIFIED: -1000,
});

module.exports = Notifications;

0 comments on commit 70c43d5

Please sign in to comment.