安装和初始化

Android 推送 SDK 是一个 AAR 格式的库。
该库可在 Maven 仓库 中获取。

此部分介绍启用和初始化 AppMetrica Push SDK 的步骤:

步骤 1. 准备您的应用

在集成 AppMetrica Push SDK 库之前,请先准备您的应用:

  1. 集成AppMetricaSDK 库,版本至少为 6.0.0
  2. 配置您的应用以发送推送通知。

步骤 2. 启用库

Push SDK 使用 OKHttp 库来缓存推送通知中显示的图片。
缓存规则从 cache-control HTTP 头获取。
如果不想缓存图片,请连接不带缓存的库。

  1. 如果你使用 Gradle 构建应用,请在应用的 Gradle 文件中添加依赖:

    带缓存
    dependencies {
        // AppMetrica Push SDK.
        implementation("io.appmetrica.analytics:push:4.3.0")
        implementation("androidx.legacy:legacy-support-v4:1.0.0")
    }
    
    dependencies {
        // AppMetrica Push SDK.
        implementation "io.appmetrica.analytics:push:4.3.0"
        implementation "androidx.legacy:legacy-support-v4:1.0.0"
    }
    
    不带缓存
    dependencies {
        // AppMetrica Push SDK.
        implementation("io.appmetrica.analytics:push:4.3.0") {
          exclude(group = "com.squareup.okhttp3", module = "okhttp")
        }
        implementation("androidx.legacy:legacy-support-v4:1.0.0")
    }
    
    dependencies {
        // AppMetrica Push SDK.
        implementation "io.appmetrica.analytics:push:4.3.0" {
         exclude group: "com.squareup.okhttp3", module: "okhttp"
        }
        implementation "androidx.legacy:legacy-support-v4:1.0.0"
    }
    
  2. 连接运输。

    1. 如果你使用 Gradle 构建应用,请将依赖项添加到应用的 Gradle 文件中:

      • 应用/build.gradle.kts

        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")
            }
        
      • app/build.gradle

        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"
            }
        

      开发中使用的版本列在 仓库 中。

    2. 使用以下方法之一初始化 Firebase:

      使用 Google Services 插件
      1. 下载配置文件google-services.json,并将其放置在项目模块目录中(例如app)。

      2. 为了文件正常工作,请在项目中启用 Google Services 插件,将以下行添加到 Gradle 文件中:

        项目

        • build.gradle.kts

          buildscript {
              dependencies {
                  classpath("com.google.gms:google-services:4.4.0")
              }
          }
          
        • build.gradle

          buildscript {
              dependencies {
                  classpath "com.google.gms:google-services:4.4.0"
              }
          }
          

        应用程序(模块)

        • 应用/build.gradle.kts

          apply(plugin = "com.google.gms.google-services")
          
        • app/build.gradle

          apply plugin: "com.google.gms.google-services"
          
      不使用插件

      在 应用 元素中修改 AndroidManifest.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 — 应用在 Firebase 中的 ID。 您可以在 Firebase 控制台 中找到它:转到 项目设置。 在 您的应用 部分,复制 应用 ID 字段的值。

      SENDER_ID — Firebase 中发送者的唯一 ID。 您可以在 Firebase 控制台 中找到它:转到 项目设置 → 云消息传递,并复制 发送者 ID 字段的值。

      API_KEY — Firebase 中的应用密钥。 您可以在 google-services.json 文件的 current_key 字段中找到它。 您可以在 Firebase 控制台 中下载该文件。

      PROJECT_ID — Firebase 中的应用 ID。 您可以在 google-services.json 文件的 project_id 字段中找到它。 您可以在 Firebase 控制台 中下载该文件。

      使用其他 Firebase 项目

      在 应用 元素中修改 AndroidManifest.xml 文件:

      <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"/>
      

      APP_ID — 应用在 Firebase 中的 ID。 您可以在 Firebase 控制台 中找到它:转到 项目设置。 在 您的应用 部分,复制 应用 ID 字段的值。

      SENDER_ID — Firebase 中发送者的唯一 ID。 您可以在 Firebase 控制台 中找到它:转到 项目设置 → 云消息传递,并复制 发送者 ID 字段的值。

      API_KEY — Firebase 中的应用密钥。 您可以在 google-services.json 文件的 current_key 字段中找到它。 您可以在 Firebase 控制台 中下载该文件。

      PROJECT_ID — Firebase 中的应用 ID。 您可以在 google-services.json 文件的 project_id 字段中找到它。 您可以在 Firebase 控制台 中下载该文件。

      注意

      您需要初始化默认的Firebase项目。

    1. 向项目添加HMS Push Kit。 开发中使用的版本列于仓库中。

    2. 如果你使用 Gradle 构建应用,请将依赖项添加到应用的 Gradle 文件中:

      • 应用/build.gradle.kts

        dependencies {
            implementation("io.appmetrica.analytics:push-provider-hms:4.3.0")
        }
        
      • app/build.gradle

        dependencies {
            implementation "io.appmetrica.analytics:push-provider-hms:4.3.0"
        }
        

      注意

      如果你只使用 HMS,请从主库中排除 Firebase 依赖:

      • 应用/build.gradle.kts

        dependencies {
            implementation("io.appmetrica.analytics:push:4.3.0") {
                exclude(group =  "io.appmetrica.analytics", module = "push-provider-firebase")
            }
        }
        
      • app/build.gradle

        dependencies {
            implementation("io.appmetrica.analytics:push:4.3.0") {
                exclude group: "io.appmetrica.analytics", module: "push-provider-firebase"
            }
        }
        
    3. 在 应用 元素中修改 AndroidManifest.xml 文件:

      <meta-data android:name="ymp_hms_default_app_id" android:value="number:APP_ID"/>
      

      APP_ID — HMS 中的应用 ID。 你可以在 app_id 字段的 agconnect-services.json 文件中找到它。 文件可以从华为控制台下载。

推送 SDK AppMetrica 可以同时与 Firebase 和 HMS 共同工作。

步骤 3. 初始化库

在应用程序中初始化库——扩展Application类,并覆盖onCreate()方法,如下所示:

Firebase 仅
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        AppMetricaPush.activate(applicationContext)
    }
}
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        AppMetricaPush.activate(getApplicationContext());
    }
}
HMS 仅
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        AppMetricaPush.activate(applicationContext, HmsPushServiceControllerProvider(this))
    }
}
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        AppMetricaPush.activate(getApplicationContext(), new HmsPushServiceControllerProvider(this));
    }
}
Firebase 和 HMS

添加以下依赖以允许调用FirebasePushServiceControllerProvider(this):

dependencies {
    implementation("io.appmetrica.analytics:push-provider-firebase:$appmetrica_push_version")
}

覆盖类:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        AppMetricaPush.activate(
            applicationContext,
            FirebasePushServiceControllerProvider(this),
            HmsPushServiceControllerProvider(this)
        )
    }
}

添加以下依赖以允许调用FirebasePushServiceControllerProvider(this):

dependencies {
    implementation "io.appmetrica.analytics:push-provider-firebase:$appmetrica_push_version"
}

覆盖类:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        AppMetricaPush.activate(
            getApplicationContext(),
            new FirebasePushServiceControllerProvider(this),
            new HmsPushServiceControllerProvider(this)
        );
    }
}

注意

必须在主进程中初始化 AppMetrica Push SDK 库。

第 4 步。 (可选) 配置静默推送通知

配置处理静默推送通知。

  1. 创建一个特殊的 BroadcastReceiver:

    class SilentPushReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            // Extract push message payload from your push message.
            val payload = intent.getStringExtra(AppMetricaPush.EXTRA_PAYLOAD)
        }
    }
    
    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(AppMetricaPush.EXTRA_PAYLOAD);
        }
    }
    
  2. 在 应用 元素中修改 AndroidManifest.xml 文件:

    <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>
    

    applicationId — Gradle 中唯一的应用 ID(包名)。
    例如,com.example.name.

第 5 步。 (可选) 启用推送令牌更新

如果您有自己的服务来处理推送通知(继承自FirebaseMessagingService 或 HmsMessageService 的类),请检查您是否没有处理来自 AppMetrica 的推送通知。

要检查推送通知不是来自 AppMetrica,请使用 AppMetricaMessagingService.isNotificationRelatedToSDK 或 AppMetricaHmsMessagingService.isNotificationRelatedToSDK 方法。

FCM 服务可能会撤回设备的推送令牌,例如当用户长时间未启动应用时。 AppMetrica 在服务器上存储推送令牌,无法向具有过时令牌的设备发送推送通知。

要自动收集当前的 push token,请在 AppMetrica 界面中进入应用设置,并启用 使用静默推送更新令牌 选项,在 推送通知 选项卡中。

第6步。 (可选) 配置与其他推送 SDK 的使用

如果您已将多个SDK集成到应用中以处理推送通知,请参考指南与其他推送服务配合使用。

需要进行额外的设置,否则可能会出现处理推送通知的问题。

发送额外信息

如果需要,您可以随推送通知一起发送额外信息。 这些数据在AppMetrica网页界面中配置推送活动时指定。

class TargetActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handlePayload(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handlePayload(intent)
    }

    private fun handlePayload(intent: Intent) {
        // Handle your payload.
        val payload = intent.getStringExtra(AppMetricaPush.EXTRA_PAYLOAD)
    }
}
public class TargetActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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(AppMetricaPush.EXTRA_PAYLOAD);
    }
}

通过推送通知检测应用启动

要区分通过打开AppMetrica推送通知启动应用和应用总启动次数,您需要检查应用的Intent action:

  • 如果您将深链路指定为操作,这将是 Intent 操作。

  • 如果将操作设置为打开应用,Intent 操作会传递值AppMetricaPush#OPEN_DEFAULT_ACTIVITY_ACTION.

设置默认图标

要设置默认的推送通知图标,请在 应用 元素中修改 AndroidManifest.xml 文件:

<meta-data android:name="io.appmetrica.analytics.push.default_notification_icon"
           android:resource="ICON_RESOURCE"/>

ICON_RESOURCE — 图标资源。
例如,@drawable/large_icon.

修改通知通道设置

您可以修改默认通知通道的设置。 例如,为它添加描述或修改其优先级。

您只能在通道创建之前更改通道设置,也就是在通过 AppMetrica 发送首次推送之前。

要获取默认通道,调用 getDefaultNotificationChannel() 方法:

AppMetricaPush.getDefaultNotificationChannel()?.apply {
    description = "Push from AppMetrica"
    importance = NotificationManager.IMPORTANCE_HIGH
    // Modify NotificationChannel properties
}
NotificationChannel channel = AppMetricaPush.getDefaultNotificationChannel();
if (channel != null) {
    channel.setDescription("Push from AppMetrica");
    channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
    // Modify NotificationChannel
}

了解更多

如果您没有找到所需答案,可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能,请附上截图。

联系支持 为文档提出改进建议
上一篇
下一篇