Push Notifications
Airship’s SDK provides a simple interface for managing push notifications within your Cordova application.
Enabling User Notifications
The Airship SDK makes a distinction between “user notifications,” which can be seen by the user, and other forms of push that allow you to send data to your app silently, or in the background. Enabling or disabling user notifications is a preference often best left up to the user, so by default, user notifications are disabled.
UAirship.setUserNotificationsEnabled(true)
Adding Custom Notification Channels (Android Only)
The Airship module also supports adding custom notification channels on Android
by supplying an XML file named ua_custom_notification_channels.xml
to your app’s
main/res/xml
directory.
The structure of this XML file follows the format used by the core Airship SDK for its default notification channels. A simple example is shown below. Note that entities and resource names with the “ua_” prefix are reserved by the Airship SDK. To create custom notifiation channels of your own, you should either use built-in android resources or supply resources defined by your app.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<NotificationChannel
id="com.myapp.my_channel"
name="@string/my_channel_name"
description="@string/my_channel_description"
importance="3" />
</resources>
Listening for Events
The JavaScript API follows an asynchronous callback approach with hooks for events such as Push registration and incoming messages.
// Register for any Airship events
document.addEventListener("urbanairship.registration", function (event) {
if (event.error) {
console.log('There was an error registering for push notifications')
} else {
console.log("Registered with ID: " + event.channelID)
}
})
// Register for any Airship push events
document.addEventListener("urbanairship.push", function (event) {
console.log("Incoming push: " + event.message)
})
Adding Custom Notification Categories
The Airship module supports adding custom notification categories on iOS, and custom notification action button groups on Android, by way of a specially named plist and XML file, respectively. Once these files are added to your iOS and Android projects, the module will load the files at runtime and automatically register your custom categories with the Airship API.
Custom categories are specified in configuration files that must be added to iOS and Android implementation via the config.xml configuration file:
<!-- Optional: include custom notification categories in plist format -->
<platform name="ios">
<resource-file src="UACustomNotificationCategories.plist" />
</platform>
<!-- Optional: include custom notification button groups in XML format -->
<platform name="android">
<resource-file src="ua_custom_notification_buttons.xml" target="app/src/main/res/xml/ua_custom_notification_buttons.xml" />
</platform>
The iOS category resource file follows the format used by the core Airship SDK for its default categories. As a starting point, see the example below. Note that key and string values prefixed with “ua_” are reserved by the Airship SDK.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>yes_no_background</key>
<array>
<dict>
<key>authenticationRequired</key>
<true/>
<key>foreground</key>
<false/>
<key>identifier</key>
<string>yes</string>
<key>title</key>
<string>Yes</string>
<key>title_resource</key>
<string>notification_button_yes</string>
</dict>
<dict>
<key>authenticationRequired</key>
<true/>
<key>foreground</key>
<false/>
<key>identifier</key>
<string>no</string>
<key>title</key>
<string>No</string>
<key>title_resource</key>
<string>notification_button_no</string>
</dict>
</array>
<key>yes_no_foreground</key>
<array>
<dict>
<key>foreground</key>
<true/>
<key>identifier</key>
<string>yes</string>
<key>title</key>
<string>Yes</string>
<key>title_resource</key>
<string>notification_button_yes</string>
</dict>
<dict>
<key>authenticationRequired</key>
<true/>
<key>foreground</key>
<false/>
<key>identifier</key>
<string>no</string>
<key>title</key>
<string>No</string>
<key>title_resource</key>
<string>notification_button_no</string>
</dict>
</array>
</dict>
</plist>
The Android category resource file should follow the following format:
As with the iOS example above, the structure of the Android category resource XML file follows the format used by the core Airship SDK for its default categories. And as before, entities and resource names with the “ua_” prefix are reserved by the Airship SDK. A similar example is given below. The icon and label resources shown are only examples; to create custom categories of your own, you should either use built-in android resources or supply resources defined by your app.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<UrbanAirshipActionButtonGroup id="yes_no_foreground">
<UrbanAirshipActionButton
foreground="true"
id="yes"
android:icon="@drawable/ic_notification_button_accept"
android:label="@string/notification_button_yes"/>
<UrbanAirshipActionButton
foreground="false"
id="no"
android:icon="@drawable/ic_notification_button_decline"
android:label="@string/notification_button_no"/>
</UrbanAirshipActionButtonGroup>
<UrbanAirshipActionButtonGroup id="yes_no_background">
<UrbanAirshipActionButton
foreground="false"
id="yes"
android:icon="@drawable/ic_notification_button_accept"
android:label="@string/notification_button_yes"/>
<UrbanAirshipActionButton
foreground="false"
id="no"
android:icon="@drawable/ic_notification_button_decline"
android:label="@string/notification_button_no"/>
</UrbanAirshipActionButtonGroup>
</resources>
Note: adding platform resource files via the config.xml file to pre-existing projects requires the respective platform to be removed and re-added for the config.xml resource file changes to take effect.
Adding HMS Notification Channels (Android Only)
Huawei Mobile Services (HMS) is the transport method that Airship supports for Huawei devices that do not have access to the Google Play Store.
Configure Airship Dashboard
Follow Huawei’s documentation to configure app information in AppGallery Connect.
Log in to AppGallery Connect.
Go to My Apps and select the app you want to configure with Airship.
Enable Push service by following Huawei’s documentation.
Go to the Develop Overview and scroll to the bottom. Copy the
App ID
andApp secret
.Configure Airship Android Channel with the HMS Credentials.
In Airship, go to Settings » Channels » Mobile App.
Click Add for Android.
Enter your Huawei app ID and app secret.
Click Add Android.
Add the HMS plugin to your app
- Add the
urbanairship-hms-cordova
plugin to your app through Cordova CLI:
cordova plugin add urbanairship-cordova-hms
- Then build your project to trigger our script that will automatically add HMS dependencies and Maven repos in your project:
cordova build
You should see these lines displayed :
UALib - Added dependency to root gradle: com.huawei.agconnect:agcp:1.4.2.301
UALib - Added maven repository to root gradle: http://developer.huawei.com/repo
UALib - Applied plugin to app gradle: com.huawei.agconnect
Categories