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

Commit 70c43d5

Browse files
author
Boris Tacyniak
authored
Merge pull request #1959 from Mookiies/addConstants
Add constants for notification importance
2 parents 4110df8 + 89303a0 commit 70c43d5

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

README.md

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ Having a problem? Read the [troubleshooting](./trouble-shooting.md) guide before
5858

5959
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).
6060

61-
When done, modify the following method in the file `AppDelegate.m`:
62-
```objective-c
63-
// Called when a notification is delivered to a foreground app.
64-
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
65-
willPresentNotification:(UNNotification *)notification
66-
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
67-
{
68-
// Still call the JS onNotification handler so it can display the new message right away
69-
NSDictionary *userInfo = notification.request.content.userInfo;
70-
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo
71-
fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];
72-
73-
// allow showing foreground notifications
74-
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
75-
// or if you wish to hide all notification while in foreground replace it with
76-
// completionHandler(UNNotificationPresentationOptionNone);
77-
}
78-
```
79-
8061
## Android manual Installation
8162

8263
**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:**
@@ -142,6 +123,17 @@ In `android/app/src/main/res/values/colors.xml` (Create the file if it doesn't e
142123
</resources>
143124
```
144125

126+
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):
127+
128+
```java
129+
@Override
130+
public void onNewIntent(Intent intent) {
131+
...
132+
super.onNewIntent(intent);
133+
...
134+
}
135+
```
136+
145137
### If you use remote notifications
146138

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

220-
return Arrays.<ReactPackage>asList(
221-
new MainReactPackage(),
222-
new ReactNativePushNotificationPackage() // <---- Add the Package
223-
);
212+
return Arrays.<ReactPackage>asList(
213+
new MainReactPackage(),
214+
new ReactNativePushNotificationPackage() // <---- Add the Package
215+
);
224216
}
225217
};
226218

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

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

599593
Available options:
600594

601-
"max" = NotficationCompat.PRIORITY_MAX
602-
"high" = NotficationCompat.PRIORITY_HIGH
603-
"low" = NotficationCompat.PRIORITY_LOW
604-
"min" = NotficationCompat.PRIORITY_MIN
595+
"max" = NotficationCompat.PRIORITY_MAX\
596+
"high" = NotficationCompat.PRIORITY_HIGH\
597+
"low" = NotficationCompat.PRIORITY_LOW\
598+
"min" = NotficationCompat.PRIORITY_MIN\
605599
"default" = NotficationCompat.PRIORITY_DEFAULT
606600

607601
More information: https://developer.android.com/reference/android/app/Notification.html#PRIORITY_DEFAULT
@@ -612,25 +606,25 @@ More information: https://developer.android.com/reference/android/app/Notificati
612606

613607
Available options:
614608

615-
"private" = NotficationCompat.VISIBILITY_PRIVATE
616-
"public" = NotficationCompat.VISIBILITY_PUBLIC
617-
"secret" = NotficationCompat.VISIBILITY_SECRET
609+
"private" = NotficationCompat.VISIBILITY_PRIVATE\
610+
"public" = NotficationCompat.VISIBILITY_PUBLIC\
611+
"secret" = NotficationCompat.VISIBILITY_SECRET
618612

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

621615
## Notification importance
622616

623-
(optional) Specify `importance` to set importance of notification. Default value: "high"
617+
(optional) Specify `importance` to set importance of notification. Default value: Importance.HIGH
618+
Constants available on the `Importance` object. `import PushNotification, {Importance} from 'react-native-push-notification';`
624619

625620
Available options:
626621

627-
"default" = NotificationManager.IMPORTANCE_DEFAULT
628-
"max" = NotificationManager.IMPORTANCE_MAX
629-
"high" = NotificationManager.IMPORTANCE_HIGH
630-
"low" = NotificationManager.IMPORTANCE_LOW
631-
"min" = NotificationManager.IMPORTANCE_MIN
632-
"none" = NotificationManager.IMPORTANCE_NONE
633-
"unspecified" = NotificationManager.IMPORTANCE_UNSPECIFIED
622+
Importance.DEFAULT = NotificationManager.IMPORTANCE_DEFAULT\
623+
Importance.HIGH = NotificationManager.IMPORTANCE_HIGH\
624+
Importance.LOW = NotificationManager.IMPORTANCE_LOW\
625+
Importance.MIN = NotificationManager.IMPORTANCE_MIN\
626+
Importance.NONE= NotificationManager.IMPORTANCE_NONE\
627+
Importance.UNSPECIFIED = NotificationManager.IMPORTANCE_UNSPECIFIED
634628

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

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
buildscript {
22
repositories {
3+
mavenCentral()
34
google()
45
jcenter()
56
}
@@ -10,6 +11,7 @@ buildscript {
1011

1112
allprojects {
1213
repositories {
14+
mavenCentral()
1315
google()
1416
jcenter()
1517
}

example/NotifService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import PushNotification from 'react-native-push-notification';
1+
import PushNotification, {Importance} from 'react-native-push-notification';
22
import NotificationHandler from './NotificationHandler';
33

44
export default class NotifService {
@@ -30,7 +30,7 @@ export default class NotifService {
3030
channelName: `Default channel`, // (required)
3131
channelDescription: "A default channel", // (optional) default: undefined.
3232
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
33-
importance: 4, // (optional) default: 4. Int value of the Android notification importance
33+
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
3434
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
3535
},
3636
(created) => console.log(`createChannel 'default-channel-id' returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
@@ -41,7 +41,7 @@ export default class NotifService {
4141
channelName: `Sound channel`, // (required)
4242
channelDescription: "A sound channel", // (optional) default: undefined.
4343
soundName: "sample.mp3", // (optional) See `soundName` parameter of `localNotification` function
44-
importance: 4, // (optional) default: 4. Int value of the Android notification importance
44+
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
4545
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
4646
},
4747
(created) => console.log(`createChannel 'sound-channel-id' returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
@@ -56,7 +56,7 @@ export default class NotifService {
5656
channelName: `Custom channel - Counter: ${this.lastChannelCounter}`, // (required)
5757
channelDescription: `A custom channel to categorise your custom notifications. Updated at: ${Date.now()}`, // (optional) default: undefined.
5858
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
59-
importance: 4, // (optional) default: 4. Int value of the Android notification importance
59+
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
6060
vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
6161
},
6262
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,14 @@ Notifications.setNotificationCategories = function() {
604604
return this.callNative('setNotificationCategories', arguments);
605605
}
606606

607+
// https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT
608+
Notifications.Importance = Object.freeze({
609+
DEFAULT: 3,
610+
HIGH: 4,
611+
LOW: 2,
612+
MIN: 1,
613+
NONE: 0,
614+
UNSPECIFIED: -1000,
615+
});
616+
607617
module.exports = Notifications;

0 commit comments

Comments
 (0)