Installation and initialization
AppMetrica Push Unity is a plugin for the Unity3d game platform that includes support for the AppMetrica Push SDK for the Android and iOS platforms.
This section describes the steps to enable and initialize the AppMetrica Unity Plugin:
Step 1. Enable the AppMetrica Unity plugin
To enable the AppMetrica Unity plugin, follow the instructions.
Note
You must use the AppMetrica Unity plugin version 6.1.0 or higher.
Stage 2. Enable the AppMetrica Push Unity plugin
The plugin is enabled via the Unity Package Manager.
Add the following dependencies to Packages/manifest.json:
{
"dependencies": {
"io.appmetrica.analytics.push": "https://github.com/appmetrica/push-unity-plugin.git#v2.0.0"
}
}
Step 3. Initialize the library
You should activate AppMetrica Push using the RuntimeInitializeOnLoadMethodAttribute attribute.
Create a static method with the [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
attribute and activate AppMetricaPush using the AppMetricaPush.Activate()
method.
Example:
using Io.AppMetrica; using Io.AppMetrica.Push; using UnityEngine; public static class AppMetricaActivator { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void Activate() { AppMetrica.Activate(new AppMetricaConfig("APIKey")); AppMetricaPush.Activate(); } }
Note
You must call AppMetricaPush.Activate()
strictly after AppMetrica.Activate()
.
Stage 4. Add settings for each native platform
-
Set up the app for using Firebase. Get and add to AppMetrica a server key for using Firebase Cloud Messaging.
-
To configure AndroidManifest.xml, make changes to the
application
element inAndroidManifest.xml
:<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"/>
APP_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 sender in Firebase. You can find it in the Firebase console: go to Project settings → Cloud Messaging and copy the value of the Sender ID field.API_KEY
— App key in Firebase. You can find it in thecurrent_key
field of thegoogle-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 thegoogle-services.json
file. You can download the file in the Firebase console.
- Add the SSL certificate to AppMetrica according to the instructions.
- To receive push notifications, ask the user for permission. We recommend using the Mobile Notifications Unity package and request permission according to the instructions.
Note
The AppMetrica Push Unity plugin uses swizzling
: it intercepts the execution of certain methods of the UnityAppController
class by using the ObjectiveC runtime. You can find the source code of the swizzling mechanism in the AMPUAppMetricaPushAppController.m file.
See also
- Configuring an Android application to send push notifications
- Configuring an iOS application to send push notifications
- Launching a push campaign
If you didn't find the answer you were looking for, you can use the feedback form to submit your question. Please describe the problem in as much detail as possible. Attach a screenshot if possible.