# Notifications

Notifications may be sent from a script.

## Sending

```javascript
import notifications from 'notifications'
```

Define custom endpoints:

```javascript
return notifications.send('c_axon_leave_study', {
  var1: 'test1',
  var2: 'test2'
}, {
  endpoints: {
    push: {
      template: 'message as template {{var1}}',
      apn: {
        topics: [
          'app',
          'voip'
        ]
      }
    }
  }
})
```

Define custom HTML:

```javascript
return notifications.send('c_axon_leave_study', {}, {
  endpoints: {
    email: {
      recipient: 'valid@example.com',
      template: null,
      message: 'testing',
      html: '<html><p>Medable&nbsp;rocks&#33;<p></html>'
    }
  }
})
```

Send a predefined notification:

```javascript
return notifications.send({
  var1: 'test1',
  var2: 'test2'
}, {
  notification: 'c_test_not'
})
```

Send an empty notification:

```javascript
import constants from 'constants'

return notifications.send({
  var1: 'test1',
  var2: 'test2'
}, {
  notification: constants.emptyId,
  endpoints: {
    email: {
      template: null,
      message: 'testing',
      html: '<html></html>'
    }
  }
})
```

Send a notification without a template or that is not predefined:

```javascript
return notifications.send({
  var1: 'test1',
  var2: 'test2'
}, {
  endpoints: {
    push: {
      message: 'test',
      apn: {
        topics: apnsTopics
      }
    }
  }
})
```

Send a Short Message Service (SMS) request to a custom mobile number using an existing template:

```javascript
return notifications.send({ age: '32' }, {
  endpoints: {
    sms: {
      template: 'c_sms_template',
      mobile: '+18004444444'
    }
  }
})
```

Send an SMS to a custom mobile number without a template:

```javascript
return notifications.send({}, {
  endpoints: {
    sms: {
      message: 'Medable rocks!',
      mobile: '+18004444444'
    }
  }
})
```

Send an SMS to a custom mobile number without a template (example 2):

```javascript
return notifications.send('Medable rocks!', {}, {
  endpoints: {
    sms: {
      mobile: '+18004444444'
    }
  }
})
```

Send an SMS to a recipient without a template:

```javascript
return notifications.send('Medable rocks!', {}, {
  endpoints: {
    sms: {}
  },
  recipient: 'valid@example.com'
})
```

Push a mobile notification to a recipient using an existing template:

```javascript
return notifications.send({
  text1: 'Medable rocks!'
}, {
  endpoints: {
    push: {
      template: 'c_push_test',
      apn: {
        topics: ['app'],
        pushType: 'alert'
      }
    }
  },
  recipient: 'valid@example.com'
})
```

Push a mobile notification to a recipient using an existing template:

```javascript
return notifications.send({}, {
  endpoints: {
    push: {
      message: 'Medable rocks!',
      apn: {
        topics: ['app'],
        pushType: 'alert'
      }
    }
  }
  recipient: 'valid@example.com'
})
```

Send an email using an existing template:

```javascript
return notifications.send({
  name: 'Adam',
  email: 'valid@example.com'
  account_id: '12345',
  study_name: 'Study',
  study_code: '00001',
  study_id: '12345'
}, {
  endpoints: {
    email: {
      recipients: [
        'valid1@example.com',
        'valid2@example.com'
      ],
      template: 'c_axon_leave_study'
    }
  }
})
```

Send an email with HTML and no template:

```javascript
return notifications.send({}, {
  endpoints: {
    email: {
      recipients: [
        'valid1@example.com',
        'valid2@example.com'
      ],
      subject: 'Medable',
      html: '<html><p>Medable&nbsp;rocks&#33;<p></html>'
    }
  }
})
```

Send an email with a plain message and no template:

```javascript
return notifications.send({}, {
  endpoints: {
    email: {
      recipients: [
        'valid1@example.com',
        'valid2@example.com'
      ],
      subject: 'Medable',
      message: 'Medable rocks!'
    }
  }
})
```

## Legacy

```javascript
const payload = {
  var1: 'test1',
  var2: 'test2'
}

notifications.send('c_axon_leave_study', payload)

notifications.send('c_axon_leave_study', payload, {
  apnsTopics: [
    'app',
    'voip'
  ],
  fcmTopic: 'all-devices',
  recipient: 'valid@example.com'
})

return
```

### send(name, variables, options)

*Arguments*

* `name` { String } Notification name
* `variables` { Object } Variables to be used in templates
* `options` { Object } \[Optional]
  * `number` { String } Sender number
  * `recipient` { ObjectID, String } ObjectID from account or email
  * `context` { Object }
  * `locale` { String } For example, 'en\_US'
  * `apiKey` { String }
  * `count` { Number } If set, sets the badge on the device icon
  * `sound` { String } If provided, overrides the default sound
  * `apnTopics` { String \[] }
  * `fcmTopic` { String \[] }
  * `pushType` { String } Applies only for Apple Push Notification (APN)

### send(payload, options)

*Arguments*

* `payload` { Object | String | Number } In case of string, used as payload only; if not, starts with `c_ or __ or not` in system notification names
* `options` { Object } \[Optional]
  * `context` { Object }
  * `locale` { String } For example, 'en\_US'
  * `apiKey` { String }
  * `endpoints` { Object }
    * `email` { Object }
      * `recipient` { ObjectID | String } ObjectID from account or email
      * `template` { String }
      * `message` { String } Plain text message
      * `html` { String } HTML message
    * `push` { Object }
      * `template` { String }
      * `message` { String }
        * `apn` { Object }
          * `topics` { String \[] }
          * `pushType` { String } &#x20;
        * `fcm` { Object }
          * `topic` { String }&#x20;
        * `tpns` { Object }&#x20;
    * `sms` { Object }
      * `mobile` { String } Recipient phone number
      * `number` { String } Sender number
      * `message` { String }

{% hint style="info" %}
View [Tencent Push Notification Service (TPNS)](https://intl.cloud.tencent.com/document/product/1024/33764) for more information about Tencent API push notifications.
{% endhint %}
