77
88React Native Push Notification API for iOS.
99
10+ | Notification | With Action | With TextInput Action |
11+ | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
12+ | <img src =" https://user-images.githubusercontent.com/6936373/97115527-77c6ee80-173a-11eb-8440-049590a25f31.jpeg " width =" 320 " /> | <img src =" https://user-images.githubusercontent.com/6936373/97115526-772e5800-173a-11eb-8b51-c5263bced07a.jpeg " width =" 320 " /> | <img src =" https://user-images.githubusercontent.com/6936373/97115522-74cbfe00-173a-11eb-9644-fc1d5e634d6b.jpeg " width =" 320 " /> |
13+
1014## Getting started
1115
1216### Install
@@ -89,11 +93,6 @@ At the top of the file:
8993Then, add the following lines:
9094
9195``` objective-c
92- // Required to register for notifications
93- - (void )application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
94- {
95- [ RNCPushNotificationIOS didRegisterUserNotificationSettings: notificationSettings ] ;
96- }
9796// Required for the register event.
9897- (void )application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
9998{
@@ -110,20 +109,13 @@ fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
110109{
111110 [ RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError: error ] ;
112111}
113- // IOS 10+ Required for localNotification event
112+ // Required for localNotification event
114113- (void )userNotificationCenter:(UNUserNotificationCenter *)center
115114didReceiveNotificationResponse:(UNNotificationResponse *)response
116115 withCompletionHandler:(void (^)(void ))completionHandler
117116{
118117 [ RNCPushNotificationIOS didReceiveNotificationResponse: response ] ;
119- completionHandler ();
120118}
121- // IOS 4-10 Required for the localNotification event.
122- - (void )application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
123- {
124- [ RNCPushNotificationIOS didReceiveLocalNotification: notification ] ;
125- }
126-
127119```
128120
129121And then in your AppDelegate implementation, add the following:
@@ -184,6 +176,57 @@ export const App = () => {
184176};
185177```
186178
179+ ## How to perform different action based on user selected action.
180+
181+ ``` js
182+ export const App = () => {
183+ const [permissions , setPermissions ] = useState ({});
184+
185+ /**
186+ * By calling this function, notification with category `userAction` will have action buttons
187+ */
188+ const setNotificationCategories = () => {
189+ PushNotificationIOS .setNotificationCategories ([
190+ {
191+ id: ' userAction' ,
192+ actions: [
193+ {id: ' open' , title: ' Open' , options: {foreground: true }},
194+ {
195+ id: ' ignore' ,
196+ title: ' Desruptive' ,
197+ options: {foreground: true , destructive: true },
198+ },
199+ {
200+ id: ' text' ,
201+ title: ' Text Input' ,
202+ options: {foreground: true },
203+ textInput: {buttonTitle: ' Send' },
204+ },
205+ ],
206+ },
207+ ]);
208+ };
209+
210+ useEffect (() => {
211+ PushNotificationIOS .addEventListener (' notification' , onRemoteNotification);
212+ });
213+
214+ const onRemoteNotification = (notification ) => {
215+ const actionIdentifier = notification .getActionIdentifier ();
216+
217+ if (actionIdentifier === ' open' ) {
218+ // Perform action based on open action
219+ }
220+
221+ if (actionIdentifier === ' text' ) {
222+ // Text that of user input.
223+ const userText = notification .getUserText ();
224+ // Perform action based on textinput action
225+ }
226+ };
227+ };
228+ ```
229+
187230# Reference
188231
189232## Methods
@@ -194,6 +237,7 @@ export const App = () => {
194237PushNotificationIOS .presentLocalNotification (details);
195238```
196239
240+ _ Deprecated_ - use ` addNotificationRequest ` instead.
197241Schedules the localNotification for immediate presentation.
198242
199243** Parameters:**
@@ -221,6 +265,7 @@ details is an object containing:
221265PushNotificationIOS .scheduleLocalNotification (details);
222266```
223267
268+ _ Deprecated_ - use ` addNotificationRequest ` instead.
224269Schedules the localNotification for future presentation.
225270
226271** Parameters:**
@@ -244,13 +289,78 @@ details is an object containing:
244289
245290---
246291
247- ### ` cancelAllLocalNotifications() `
292+ ### ` addNotificationRequest() `
293+
294+ ``` jsx
295+ PushNotificationIOS .addNotificationRequest (request);
296+ ```
297+
298+ Sends notificationRequest to notification center at specified firedate.
299+ Fires immediately if firedate is not set.
300+
301+ ** Parameters:**
302+
303+ | Name | Type | Required | Description |
304+ | ------- | ------ | -------- | ----------- |
305+ | request | object | Yes | See below. |
306+
307+ request is an object containing:
308+
309+ - ` id ` : Identifier of the notification. Required in order to be able to retrieve specific notification. (required)
310+ - ` title ` : A short description of the reason for the alert.
311+ - ` subtitle ` : A secondary description of the reason for the alert.
312+ - ` body ` : The message displayed in the notification alert.
313+ - ` badge ` The number to display as the app's icon badge. Setting the number to 0 removes the icon badge.
314+ - ` fireDate ` : The date and time when the system should deliver the notification.
315+ - ` repeats ` : Sets notification to repeat daily. Must be used with fireDate.
316+ - ` sound ` : The sound played when the notification is fired.
317+ - ` category ` : The category of this notification, required for actionable notifications.
318+ - ` isSilent ` : If true, the notification will appear without sound.
319+ - ` userInfo ` : An object containing additional notification data.
320+
321+ ---
322+
323+ ### ` setNotificationCategories() `
324+
325+ ``` jsx
326+ PushNotificationIOS .setNotificationCategories (categories);
327+ ```
328+
329+ Sets category for the notification center.
330+ Allows you to add specific actions for notification with specific category.
331+
332+ ** Parameters:**
333+
334+ | Name | Type | Required | Description |
335+ | ---------- | -------- | -------- | ----------- |
336+ | categories | object[ ] | Yes | See below. |
337+
338+ ` category ` is an object containing:
339+
340+ - ` id ` : Identifier of the notification category. Notification with this category will have the specified actions. (required)
341+ - ` actions ` : An array of notification actions to be attached to the notification of category id.
342+
343+ ` action ` is an object containing:
344+
345+ - ` id ` : Identifier of Action. This value will be returned as actionIdentifier when notification is received.
346+ - ` title ` : Text to be shown on notification action button.
347+ - ` options ` : Options for notification action.
348+ - ` foreground ` : If ` true ` , action will be displayed on notification.
349+ - ` destructive ` : If ` true ` , action will be displayed as destructive notification.
350+ - ` authenticationRequired ` : If ` true ` , action will only be displayed for authenticated user.
351+ - ` textInput ` : Option for textInput action. If textInput prop exists, then user action will automatically become a text input action. The text user inputs will be in the userText field of the received notification.
352+ - ` buttonTitle ` : Text to be shown on button when user finishes text input. Default is "Send" or its equivalent word in user's language setting.
353+ - ` placeholder ` : Placeholder for text input for text input action.
354+
355+ ---
356+
357+ ### ` remooveAllPendingNotificationRequests() `
248358
249359``` jsx
250- PushNotificationIOS .cancelAllLocalNotifications ();
360+ PushNotificationIOS .remooveAllPendingNotificationRequests ();
251361```
252362
253- Cancels all scheduled localNotifications
363+ Removes all pending notification requests in the notification center.
254364
255365---
256366
0 commit comments