Installation and initialization

AppMetrica Push Cordova is a plugin for the Cordova (PhoneGap) platform that includes support for the AppMetrica Push SDK for Android and iOS through the JavaScript interface.

Before using the AppMetrica Push Cordova plugin, you need to enable and initialize the AppMetrica Cordova plugin version 0.2.0 or later.

  1. Integrating the plugin
  2. Connection examples
  3. API methods

Integrating the plugin

Step 1. Add the supported platforms to your project

Step 2. Add the plugin to the project by using one of the following console commands:
cordova plugin add yandex-appmetrica-push-plugin-cordova
or
cordova plugin add https://github.com/yandexmobile/metrica-push-plugin-cordova.git

When you add another supported platform to the project, the plugin automatically downloads the corresponding SDK library.

Step 3. (For Android projects only) Add the following to the application element of the AndroidManifest.xml file:

<meta-data android:name="ymp_gcm_project_number" android:value="number:SENDER_ID"/>
For example:
  1. Add the cordova-custom-config plugin:
    cordova plugin add cordova-custom-config
  2. Make the following changes to the config.xml file of the android project:
    <config-file parent="./application" target="AndroidManifest.xml">
      <meta-data android:name="ymp_gcm_project_number" android:value="number:SENDER_ID" />
    </config-file>
What is SENDER_ID?

SENDER_ID — Unique app identifier in GCM (Google Cloud Messaging).

Connection examples

In the example below:

  • The JavaScript object with the configuration is created.
  • The AppMetrica Cordova plugin is activated.
  • The AppMetrica Push Cordova plugin is initialized.
  • The push token of the device is output to the log.
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady () {
  var configuration = {
    apiKey: 'Your API key here'
  }
  window.appMetrica.activate(configuration);
  
  window.appMetricaPush.init();
  window.appMetricaPush.getToken(function (token) {
    console.log("Token: " + token);
  });
}
What is the API key?

The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.

API methods

In the code, use window.appMetricaPush for accessing AppMetrica Push.

init()

init()

Initializes the AppMetrica Push Cordova plugin.

getToken()

getToken(function (token) {
  //token has the String type
})

Returns the push token of the device.