Connecting the AppMetrica Push SDK
The Push SDK for Android is a library in the AAR format. The library is available in the Maven repository.
This section describes the steps to enable and initialize AppMetrica Push SDK:
Step 1. Prepare your app
Before integrating the AppMetrica Push SDK library, prepare your app:
- Integrate the AppMetrica SDK library at least version 3.0.0.
- Configure your app for sending push notifications.
Step 2. Enable the library
Since version 1.4.0 to the Push SDK added the OKHttp library. The SDK uses it for caching images that are displayed in push notifications. Caching rules are taken from the cache-control
HTTP header. If you do not want the images to be cached, connect the library without caching.
- In the build.gradle file add the following dependencies in the
dependencies
block:dependencies { ... implementation "com.yandex.android:mobmetricapushlib:2.3.2" implementation "androidx.legacy:legacy-support-v4:1.0.0" ... }
Copied to clipboarddependencies { ... implementation ("com.yandex.android:mobmetricapushlib:2.3.2") { exclude group: 'com.squareup.okhttp3', module: 'okhttp' } implementation "androidx.legacy:legacy-support-v4:1.0.0" ... }}
Copied to clipboard Connect the transport.
- In the build.gradle file, add Firebase dependencies in the
dependencies
block:dependencies { ... // minimum support version 20.3.0 implementation "com.google.firebase:firebase-messaging:22.0.0" implementation "com.google.android.gms:play-services-base:17.5.0" ... }
Copied to clipboard Initialize Firebase using one of the following methods:
- Download the configuration file google-services.json and put it in the project module's directory (such as app).
In order to work with the file correctly, enable the Google Services plugin in the project by adding the following lines to the build.gradle file:
- project
-
buildscript{ ... dependencies { ... classpath 'com.google.gms:google-services:4.3.4' ... } ... }
Copied to clipboard - application (module)
-
// In the end of the file. apply plugin: 'com.google.gms.google-services'
Copied to clipboard
Using Google Services PluginWithout using the pluginMake changes in the
application
element in the AndroidManifest.xml file:<meta-data android:name="ymp_firebase_default_app_id" android:value="APP_ID"/> <meta-data android:name="ymp_gcm_default_sender_id" android:value="number:SENDER_ID"/> <meta-data android:name="ymp_firebase_default_api_key" android:value="API_KEY"/> <meta-data android:name="ymp_firebase_default_project_id" android:value="PROJECT_ID"/>
Copied to clipboardAPP_ID
— ID of the app in Firebase. You can find it in the Firebase console: go to the Project settings. In the Your application section copy the value of the application ID field.SENDER_ID
— The unique ID of the sender in Firebase. You can find it in the Firebase console: go to and copy the value of the Sender ID field.API_KEY
— App key in Firebase. You can find it in thecurrent_key
field of the google-services.json file. You can download the file in the Firebase console.PROJECT_ID
— App ID in Firebase. You can find it in theproject_id
field of the google-services.json file. You can download the file in the Firebase console.Using with other Firebase projectsMake changes in the
application
element in the AndroidManifest.xml file:<meta-data android:name="ymp_firebase_app_id" android:value="APP_ID"/> <meta-data android:name="ymp_gcm_sender_id" android:value="number:SENDER_ID"/> <meta-data android:name="ymp_firebase_api_key" android:value="API_KEY"/> <meta-data android:name="ymp_firebase_project_id" android:value="PROJECT_ID"/>
Copied to clipboardAPP_ID
— ID of the app in Firebase. You can find it in the Firebase console: go to the Project settings. In the Your application section copy the value of the application ID field.SENDER_ID
— The unique ID of the sender in Firebase. You can find it in the Firebase console: go to and copy the value of the Sender ID field.API_KEY
— App key in Firebase. You can find it in thecurrent_key
field of the google-services.json file. You can download the file in the Firebase console.PROJECT_ID
— App ID in Firebase. You can find it in theproject_id
field of the google-services.json file. You can download the file in the Firebase console.Attention. You should initialize default Firebase project.
- Add HMS Push Kit to the project.
- In the build.gradle file add the following dependencies in the
dependencies
block:dependencies { ... implementation "com.yandex.android:appmetricapush-provider-hms:2.3.2" ... }
Copied to clipboardAttention. If you're only going to use HMS, exclude the Firebase dependency from the main library:dependencies { ... implementation ("com.yandex.android:mobmetricapushlib:2.3.2") { exclude group: 'com.yandex.android', module: 'appmetricapush-provider-firebase' } ... }
Copied to clipboard Make changes in the
application
element in the AndroidManifest.xml file:<meta-data android:name="ymp_hms_default_app_id" android:value="number:APP_ID"/>
Copied to clipboardAPP_ID
— App ID in HMS. You can find it in theapp_id
field of the agconnect-services.json file. You can download the file in theHuawei console.
The AppMetrica Push SDK can simultaneously work with Firebase and HMS.
Step 3. Initialize the library
Initialize the library in the app — extend the Application
class and override the onCreate()
method as follows:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); YandexMetricaPush.init(getApplicationContext()); } }
Copied to clipboard
class MyApp : Application() { override fun onCreate() { super.onCreate() YandexMetricaPush.init(applicationContext) } }
Copied to clipboard
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); YandexMetricaPush.init(getApplicationContext(), new HmsPushServiceControllerProvider(this)); } }
Copied to clipboard
class MyApp : Application() { override fun onCreate() { super.onCreate() YandexMetricaPush.init(applicationContext, HmsPushServiceControllerProvider(this)) } }
Copied to clipboard
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); YandexMetricaPush.init( getApplicationContext(), new FirebasePushServiceControllerProvider(this), new HmsPushServiceControllerProvider(this) ); } }
Copied to clipboard
class MyApp : Application() { override fun onCreate() { super.onCreate() YandexMetricaPush.init( applicationContext, FirebasePushServiceControllerProvider(this), HmsPushServiceControllerProvider(this) ) } }
Copied to clipboard
Step 4. (Optional) Configure Silent Push Notifications
Configure processing silent push notifications.
Create a special
BroadcastReceiver
:import com.yandex.metrica.push.YandexMetricaPush; public class SilentPushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Extract push message payload from your push message. String payload = intent.getStringExtra(YandexMetricaPush.EXTRA_PAYLOAD); ... } }
Copied to clipboardMake changes in the
application
element in the AndroidManifest.xml file:<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application> ... <receiver android:name=".SilentPushReceiver"> <intent-filter> <!-- Receive silent push notifications. --> <action android:name="${applicationId}.action.ymp.SILENT_PUSH_RECEIVE"/> </intent-filter> </receiver> ... </application> </manifest>
Copied to clipboardapplicationId
— Unique app ID in Gradle (package name). For example, com.example.name.
Step 5. (Optional) Enable push tokens update
If you have your own service to handle push notifications (a class inherited from FirebaseMessagingService
or HmsMessageService
), check that you are not handling push notifications from AppMetrica.
To check that the push notification is not from AppMetrica, use the MetricaMessagingService.isNotificationRelatedToSDK or MetricaHmsMessagingService.isNotificationRelatedToSDK method.
The FCM service can withdraw the push token of the device, for example, if the user did not launch the application for a long time. AppMetrica stores push tokens on the server and can not send a push notification to a device with an obsolete token.
To automatically collect current push token go to the application settings in the AppMetrica interface and enable the Update tokens with a Silent Push notification option in the Push Notifications tab.
Sending additional information
You can send additional information with the push notification if necessary. This data is specified in the AppMetrica web interface when configuring the push campaign.
public class TargetActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saveInstanceState); handlePayload(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handlePayload(intent); } private void handlePayload(Intent intent) { // Handle your payload. String payload = intent.getStringExtra(YandexMetricaPush.EXTRA_PAYLOAD); ... } }
Copied to clipboard
Detecting the application launch via push notification
To distinguish app launches initiated by opening an AppMetrica push notification from the total number of app starts, you should check the Intent action of the app. If you specified a deeplink as the action, this will be the Intent action. If the action is set as opening the app, the Intent action passes the value YandexMetricaPush#OPEN_DEFAULT_ACTIVITY_ACTION
.
Setting the default icon
To set the default push notification icon, make changes in the application element in the AndroidManifest.xml file:
<meta-data android:name="com.yandex.metrica.push.default_notification_icon"
android:resource="ICON_RESOURCE"/>
ICON_RESOURCE
— Icon resource. For example, @drawable/large_icon
.