Detox supports mocking user notifications for iOS apps.
Note: The mocking mechanism will not mimic the UI of a user notification. Instead, it will only simulate a user interaction with the notification.
Using launchApp()
with custom params (e.g., userNotification
) will trigger the mocking mechanism.
await device.launchApp({newInstance: true, userNotification: notification});
Example:
describe('Background push notification', () => {
beforeEach(async () => {
await device.launchApp({
newInstance: true,
userNotification: userNotificationPushTrigger,
});
});
it('push notification from background', async () => {
await expect(element(by.text('From push'))).toBeVisible();
});
});
Use the sendUserNotification()
method to send notification to running app. Notifications can be sent to an active or a background app.
await device.sendUserNotification(notification)
Example:
describe('Foreground user notifications', () => {
beforeEach(async () => {
await device.launchApp();
});
it('Local notification from inside the app', async () => {
await device.sendUserNotification(localNotification);
await expect(element(by.text('from local notificaton'))).toBeVisible();
});
});
User notifications are passed as JSON objects to Detox. The JSON object needs to provide some required data, but can also provide additional, optional data.
Key | Required | Value Type | Description |
---|---|---|---|
trigger |
Yes | Object | The conditions that trigger the delivery of the notification. See the Triggers section below. |
title |
No | String | A short description of the reason for the alert. |
subtitle |
No | String | A secondary description of the reason for the alert. |
body |
No | String | The body of the notification. |
badge |
No | Integer | The number to display as the app’s icon badge. |
payload |
No | Object | An object of custom information associated with the notification. |
category |
No | String | The identifier of the app-defined category object. |
content-available |
No | Integer | Include this key with a value of 1 to configure a silent notification. |
user-text |
No | String | The text response provided by the user. |
action-identifier |
No | String | The identifier for the action that the user selected. |
Triggers are objects representing the trigger.
Key | Required | Value Type | Description |
---|---|---|---|
type |
Yes | String | The conditions that trigger the delivery of the notification. See the Trigger Types section below. |
repeats |
No | Boolean | Indicates whether the event repeats. Only used for calendar , timeInterval and location trigger types. |
timeInterval |
Yes for timeInterval trigger type |
Number | The time interval used to create the trigger. |
date-components |
Yes for calendar trigger type |
Object | The date components used to construct this object. See the Date Components section below. |
region |
Yes for location trigger type |
Object | The region used to determine when the notification is sent. See the Region section below. |
There are four types of triggers supported by Detox at this time:
push
calendar
timeInterval
location
For convenience, these trigger types are provided as constants in DetoxConstants
:
const DetoxConstants = require('detox').DetoxConstants;
const userNotification = {
"trigger": {
"type": DetoxConstants.userNotificationTriggers.push
},
...
}
Key | Required | Value Type | Description |
---|---|---|---|
era |
No | Integer | The number of era units for the receiver. |
year |
No | Integer | The number of year units for the receiver. |
month |
No | Integer | The number of month units for the receiver. |
day |
No | Integer | The number of day units for the receiver. |
hour |
No | Integer | The number of hour units for the receiver. |
minute |
No | Integer | The number of minute units for the receiver. |
second |
No | Integer | The number of second units for the receiver. |
weekday |
No | Integer | The number of the weekday unit for the receiver. |
weekdayOrdinal |
No | Integer | The ordinal number of weekday units for the receiver. |
quarter |
No | Integer | The number of quarters for the receiver. |
weekOfMonth |
No | Integer | The week number of the month for the receiver. |
leapMonth |
No | Boolean | Indicates whether the month is a leap month. |
Key | Required | Value Type | Description |
---|---|---|---|
center |
Yes | Object | The center point of the geographic area. See the Coordinate section below. |
radius |
Yes | Number | The radius (measured in meters) that defines the geographic area’s outer boundary. |
notifyOnEntry |
No | Boolean | Indicates that notifications are generated upon entry into the region. |
notifyOnExit |
No | Boolean | Indicates that notifications are generated upon exit from the region. |
Key | Required | Value Type | Description |
---|---|---|---|
latitude |
Yes | Number | The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator. |
longitude |
Yes | Number | The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian. |