Notification Actions allow the user to interact with the push notification. Those shiny buttons that show up when you swipe a notification to the left on your lock screen, or pull down a notification that appears on the top of the screen? Each one of those buttons is an action.
Notification Categories allow you to group multiple actions together. This Category is what you'll ultimately associate with a push notification.
The basic workflow is:
- Create and configure some actions.
- Group them together into a category.
- (optional) Implement some appdelegate methods to respond to actions.
- Show a local (or remote) notification, and associate it with the category from (2) to show your actions.
- Follow the instructions here to set up push notifications in your app.
import NotificationActions from 'react-native-notification-actions'
// Create an "upvote" action that will display a button when a notification is swiped
let upvoteButton = new NotificationActions.Action({
activationMode: 'background',
title: 'Upvote',
identifier: 'UPVOTE_ACTION'
}, (source, done) => {
console.info('upvote button pressed from source: ', source);
done(); //important!
});
// Create a "comment" button that will display a text input when the button is pressed
let commentTextButton = new NotificationActions.Action({
activationMode: 'background',
title: 'Reply',
behavior: 'textInput',
identifier: 'REPLY_ACTION'
}, (source, done, text) => {
console.info('reply typed via notification from source: ', source, ' with text: ', text);
done(); //important!
});
// Create a category containing our two actions
let myCategory = new NotificationActions.Category({
identifier: 'something_happened',
actions: [upvoteButton, commentTextButton],
forContext: 'default'
});
NotificationActions.updateCategories([myCategory]);
// ** important ** update the categories
NotificationActions.updateCategories([myCategory]);
title
- [String] Title for the action button [apple doc]identifier
- [String] Identifier for the action.onComplete
- [function(responseData, completionHandler)] Called when the your app has been activated by the user interacting with this action.responseData
contains inputted text ifbehavior
is set totextInput
. Important - you must callcompletionHandler
when you are done handling the action.behavior
- [String] Custom behavior for the action [apple doc]default
(default) - No additional behaviorstextInput
- When tapped, this action opens a text input. On completion, the text is delivered to youronComplete
callback.
activationMode
- [String] What should iOS do with this app when this action is pressed? [apple doc]foreground
(default) - Bring the app into the foregroundbackground
- Launch the app in the background
authenticationRequired
- [Boolean] Should the user be required to unlock the device before the action is performed? [apple doc]destructive
- [Boolean] If true, displays the action button differently (for example, colored red). [apple doc]
identifier
- [String] Identifier for the category. When creating a local or remote notification, use this value to associate this category with that notification. [apple doc]actions
- [Array ofAction
s] The actions to display with this category. Maximum length depends on the value of thecontext
property. [apple doc]context
- [String] Controls how many actions to display with a notification. [apple doc]default
(default) - Displays up to 4 actions.minimal
- Displays up to 2 actions.
- implement action parameters
- PR react-native to allow "category" key in local notification payloads (and maybe other keys as well)
- Can we just use a string value for local notifications, or do we have to use the actual string variable as suggested here?