NativeScript Urban Airship plugin
Successfully Tested on the following configurations
test case
- tns cli version 2.5.0
- tns-core-modules 2.4.0
- android 2.4.1
- ios 2.4.0
test case
- tns cli version 2.5.0
- tns-core-modules 2.5.0
- android 2.5.0
- ios 2.5.0
Steps to integrate into your main project
iOS SDK
based Urban Airship SDK 8.1.4
Urban Airship iOS documentation
Android SDK
based Urban Airship SDK 8.2.5
Urban Airship Android documentation
Setup
Steps to integrate into your main project
tns plugin add nativescript-urban-airship
Create in your app folder a file named urbanAirshipSettings.ts
(filename optional)
app/urbanAirshipSettings.ts
import { UrbanAirshipSettings } from 'nativescript-urban-airship';
export const urbanAirshipSettings: UrbanAirshipSettings = {
developmentAppKey: "Your Development App Key",
developmentAppSecret: "Your Development App Secret",
gcmSender: "Your Google API Project Number",
detectProvisioningMode: false,
inProduction: false,
productionAppKey: "Your Production App Key",
productionAppSecret: "Your Production Secret"
};
app/push-notification.ts
import { ApplicationEventData, on, launchEvent, resumeEvent, ios } from 'application';
import { NsUrbanairship } from 'nativescript-urban-airship';
import { urbanAirshipSettings } from './urbanAirshipSettings';
export class PushNotification {
public static initialize(): void {
if (ios) {
on(launchEvent, (args: ApplicationEventData) => {
NsUrbanairship.getInstance().startUp(urbanAirshipSettings);
});
on(resumeEvent, (args: ApplicationEventData) => {
NsUrbanairship.getInstance().resetBadgeCount();
});
}
}
}
app/app.ts
import { PushNotification } from './push-notification';
PushNotification.initialize();
PushNotification registration for Android
Registering the Urban Airship's takeoff at the correct moment is crucial for Android. That is why the implementation differs a bit compared to iOS.
Android in particular requires the takeoff to be called on the onCreate lifeCycle of the application for it to register the Broadcast receivers in time.
Extend your application using the NativeScript documentation
This is appliciable for NativeScript versions 2.3 - 2.5
app/application.android.ts
import * as application from 'application';
import { urbanAirshipSettings } from './urbanAirshipSettings';
declare const com: any;
@JavaProxy('com.tns.YourApplicationName')
class YourApplicationName extends android.app.Application {
public onCreate(): void {
super.onCreate();
const options = new com.urbanairship.AirshipConfigOptions.Builder()
.setDevelopmentAppKey(urbanAirshipSettings.developmentAppKey)
.setDevelopmentAppSecret(urbanAirshipSettings.developmentAppSecret)
.setProductionAppKey(urbanAirshipSettings.productionAppKey)
.setProductionAppSecret(urbanAirshipSettings.productionAppSecret)
.setInProduction(urbanAirshipSettings.inProduction)
.setGcmSender(urbanAirshipSettings.gcmSender)
.build();
com.urbanairship.UAirship.takeOff(application.android.context, options);
}
}
app/App_Resources/Android/AndroidManifest.xml
<application
android:name="com.tns.YourApplicationName"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
API
export interface CommonUrbanAirship {
startUp(urbanAirshipSettings: UrbanAirshipSettings): void;
registerUser(userId: string): void;
notificationOptIn(): Promise<boolean>;
isOptIn(): boolean;
getChannelID(): string;
notificationOptOut(): Promise<boolean>;
unRegisterUser(): void;
resetBadgeCount(): void;
getRegistrationToken(): string;
}
Usage Example
NsUrbanairship.getInstance().isOptIn();
NsUrbanairship.getInstance().unRegisterUser();