使用 Bloomreach 轻松实现 Android 推送通知

发布: (2025年12月30日 GMT+8 11:01)
3 min read
原文: Dev.to

Source: Dev.to

Aziz

推送通知

介绍

推送通知帮助您的应用有效地吸引用户。本文档阐述了如何在 Android 上使用 Bloomreach(前称 Exponea)实现推送通知——从空白项目到功能完整的实现。

前置条件

  • 用 Kotlin 编写的 Android 应用
  • 已配置 Firebase Cloud Messaging (FCM)
  • 已启用推送的 Bloomreach 账户
  • 对 Android 应用生命周期有基本了解

高级架构

  1. 应用向 FCM 注册并获取设备令牌。
  2. 令牌被发送至 Bloomreach SDK。
  3. Bloomreach 通过 FCM 发送推送活动。
  4. FCM 将消息投递到设备。
  5. 应用显示通知。

Firebase 设置

  1. 将 Firebase 添加到您的项目,通过 Firebase 控制台。
  2. google-services.json 放置在 app/ 文件夹中。

Gradle 依赖

implementation platform('com.google.firebase:firebase-bom:33.0.0')
implementation 'com.google.firebase:firebase-messaging-ktx'

应用 Google Services 插件

plugins {
  id 'com.google.gms.google-services'
}

集成 Bloomreach SDK(前称 Exponea)

第 1 步 – 添加 SDK

implementation("com.exponea.sdk:sdk:4.8.1")

第 2 步 – 初始化 SDK

从 Bloomreach Engagement 门户(项目设置 → 访问管理 → API)获取 projectTokenauthorizationbaseURL
在初始化之前,请确保已获得用户跟踪同意。

val configuration = ExponeaConfiguration().apply {
    authorization = "Token YOUR_API_KEY"
    projectToken = "YOUR_PROJECT_TOKEN"
    baseURL = "https://api.exponea.com"
}
Exponea.init(this, configuration)

第 3 步 – 在 Application 子类中放置 SDK 初始化

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        val configuration = ExponeaConfiguration().apply {
            authorization = "Token YOUR_API_KEY"
            projectToken = "YOUR_PROJECT_TOKEN"
            baseURL = "https://api.exponea.com"
        }

        Exponea.init(this, configuration)
    }
}

AndroidManifest.xml 中注册自定义的 Application 类:

<!-- Add your application tag here -->

推送通知设置

在 Bloomreach 中启用推送通知

  • Bloomreach Engagement 通过 场景 发送通知。
  • 通知可以是标准提醒,也可以是静默(后台)消息。

实现 Firebase 消息服务

import android.app.NotificationManager
import android.content.Context
import com.exponea.sdk.Exponea
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {

    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)
        // Let Bloomreach handle the message; return false if it’s from another provider
        if (!Exponea.handleRemoteMessage(applicationContext, message.data, notificationManager)) {
            // Handle non‑Bloomreach messages here
        }
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Exponea.handleNewToken(applicationContext, token)
    }
}

AndroidManifest.xml 中注册服务:

<!-- Add your service declaration here -->

在 Bloomreach 中配置 FCM

  1. 在 Google Cloud 中创建服务账户并生成私钥(JSON)。
  2. 在 Bloomreach Engagement 中,使用该 JSON 密钥添加 Firebase Cloud Messaging 集成

Dashboard

Project Settings → Channels → Push Notifications → Firebase Cloud Messaging Integration 中选择该集成

Integration

完成设置后,您的应用将接收通过 Bloomreach 发送的推送通知,且令牌跟踪将自动处理。

指南结束。

Back to Blog

相关文章

阅读更多 »

新年快乐,社区!

引言 大家好,感谢阅读本博客,祝大家新年快乐! 项目概述 本项目的最终目标是一个纯粹的...