# Push Notifications for the Cordova Plugin

How to configure your application to receive and respond to notifications.

## Platform Setup

Before you can send and receive push notifications, you need to configure your app for the platform(s) you're targeting. Follow the platform-specific setup instructions below.

### iOS

#### Enable Push Notifications Capability

1. Open your project in Xcode.
2. Click on your project in the Project Navigator.
3. Select your main app target and then click the **Signing & Capabilities** tab.
4. If you do not see Push Notifications enabled, click **+ Capability** and add **Push Notifications**.

    ![Push Notifications for the Cordova Plugin](https://www.airship.com/docs/images/ios-enable-push-notifications-capabilities_hu_2e1789fffb02612b.webp)

#### Enable Background Modes

1. Select your main app target and then click the **Signing & Capabilities** tab.
2. Click **+ Capability** and add **Background Modes**.

    ![Push Notifications for the Cordova Plugin](https://www.airship.com/docs/images/ios-enable-background-mode-capabilities_hu_f135d9fec0ba0d06.webp)

3. In the **Background Modes** section, select the **Remote notifications** checkbox.

    ![Push Notifications for the Cordova Plugin](https://www.airship.com/docs/images/ios-background-mode-remote-notifications_hu_7e38b08288fcd7b2.webp)

#### Signing

Add your Apple Developer Account Team ID to the [build.json](https://cordova.apache.org/docs/en/latest/guide/platforms/ios/#using-buildjson).

```json
{
  "ios": {
    "debug": {
    "developmentTeam": "XXXXXXXXXX"
    },
    "release": {
      "developmentTeam": "XXXXXXXXXX"
    }
  }
}
```


Your iOS builds will need to reference the `build.json` using Cordova's `--buildConfig` flag.

#### Notification Service Extension

<p>To take advantage of notification attachments, such as images,
animated gifs, and video, you will need to create a <a href="https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications" target="_blank">notification service extension<span class="external-link-icon">&thinsp;<i class="ph ph-arrow-square-out"></i></span></a>.</p> Follow the steps in the [iOS Notification Service Extension Guide](https://www.airship.com/docs/developer/sdk-integration/apple/push-notifications/notification-service-extension/).

### Android

Configure Firebase Cloud Messaging (FCM) to enable push notifications on Android.

#### FCM Setup

Add a reference to your `google-services.json` file in the app's `config.xml` and enable Google Services plugin:

```xml
<preference name="AndroidGradlePluginGoogleServicesEnabled" value="true" />

<platform name="android">
    ...
    <resource-file src="google-services.json" target="app/google-services.json" />
</platform>
```


#### Notification Configuration

Configure the notification icon and accent color in your `takeOff` config:

```javascript
Airship.takeOff({
  production: {
    appKey: "<APP_KEY>",
    appSecret: "<APP_SECRET>"
  },
  development: {
    appKey: "<APP_KEY>",
    appSecret: "<APP_SECRET>"
  },
  inProduction: true, 
  site: "us",
  android: {
      notificationConfig: {
          icon: "ic_notification",
          accentColor: "#00ff00"
      }
  }
})
```


See the [Cordova Plugin Setup guide](https://www.airship.com/docs/developer/sdk-integration/cordova/installation/getting-started/) for complete `takeOff` configuration options.

## Enable User Notifications

The Airship SDK distinguishes between *user notifications* (visible to users) and *silent push notifications* (background data delivery). User notifications require explicit permission from the user.

By default, user notifications are disabled. Enable them when you want to show visible notifications to users.

### Basic Enablement

The simplest way to enable user notifications is with `setUserNotificationsEnabled()`:

```javascript
Airship.push.setUserNotificationsEnabled(true)
```


This will prompt the user for permission if not already granted. However, it does not provide feedback on whether the user accepted or denied the permission.

> **Note:** For apps that target Android 13 (API 33) and above, enabling user notifications will display a runtime permission prompt.
> 
> To increase the likelihood that the user will accept, you should avoid prompting the user for permission immediately on app startup, and instead wait for a more appropriate time to prompt for notification permission.


### Async Enablement

For more control over the permission flow, use `enableUserNotifications()` which returns the permission result:

```javascript
Airship.push.enableUserNotifications((granted) => {
  if (granted) {
    console.log('Notifications enabled')
  } else {
    console.log('Notifications denied')
  }
})
```


### Checking Notification Status

To check if user notifications are currently enabled:

```javascript
Airship.push.isUserNotificationsEnabled((enabled) => {
  console.log('User notifications enabled:', enabled)
})
```


For more detailed status information, use `getNotificationStatus()`:

```javascript
Airship.push.getNotificationStatus((status) => {
  console.log('Are notifications allowed:', status.areNotificationsAllowed)
  console.log('Is opted in:', status.isOptedIn)
})
```


To monitor notification status changes in real-time:

```javascript
const subscription = Airship.push.onNotificationStatusChanged((event) => {
  console.log('Notification status changed:', event.status)
  console.log('Is opted in:', event.status.isOptedIn)
})

// Later, to cancel the subscription
subscription.cancel()
```


### Getting the Push Token

To get the platform-specific push token (APNs token on iOS, FCM token on Android):

```javascript
Airship.push.getPushToken((token) => {
  console.log('Push token:', token)
})

// Or listen for token updates
const subscription = Airship.push.onPushTokenReceived((event) => {
  console.log('Push token:', event.pushToken)
})
```


If push notifications aren't working as expected, see [Troubleshooting Push Notifications](https://www.airship.com/docs/developer/sdk-integration/cordova/troubleshooting/push-notifications/) to check notification status and fix common issues.
