支持推送通知
通过将 Firebase Cloud Messaging (FCM) 与 Singular SDK 集成,跟踪用户与推送通知的互动,以衡量再参与活动并准确归因于转化。
请遵循以下实施指南,以确保通知数据正确传递到 Singular SDK,从而获得正确的归因。
为什么要跟踪推送通知?推送通知能推动用户重新参与,但跟踪需要正确的整合。Singular 可确保与通知互动的用户得到正确归因,从而优化营销活动和参与策略。
实施指南
处理 FCM 通知
覆盖 FirebaseMessagingService 中的onMessageReceived() 方法,以便在推送消息到达时捕获通知数据。
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
var title = ""
var body = ""
message.notification?.let {
Log.d("singular-app", it.toString())
title = it.title ?: ""
body = it.body ?: ""
}
val data: Map<String, String> = message.data
if (data.isNotEmpty()) {
Log.d("singular-app", data.toString())
}
// Forward payload data to intent
processNotification(title, body, data)
}
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
String title = "";
String body = "";
if (message.getNotification() != null) {
Log.d("singular-app", message.getNotification().toString());
title = message.getNotification().getTitle();
body = message.getNotification().getBody();
}
Map<String, String> data = message.getData();
if (!data.isEmpty()) {
Log.d("singular-app", data.toString());
}
// Forward payload data to intent
processNotification(title, body, data);
}
最佳实践:同时捕获通知内容(标题、正文)和数据有效载荷,以确保有完整的跟踪信息用于归因。
处理和转发通知数据
创建一个意图,启动附加通知有效载荷的 MainActivity,确保 Singular 接收到跟踪数据。
private fun processNotification(title: String, body: String, data: Map<String, String>) {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
// Attach notification data to the intent
data.forEach { (key, value) ->
putExtra(key, value)
}
}
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notificationBuilder = NotificationCompat.Builder(this, "your_channel_id")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
notificationManager?.notify(0, notificationBuilder.build())
}
private void processNotification(String title, String body, Map<String, String> data) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Attach notification data to the intent
for (Map.Entry<String, String> entry : data.entrySet()) {
intent.putExtra(entry.getKey(), entry.getValue());
}
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "your_channel_id")
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, notificationBuilder.build());
}
}
Android 12+ 要求:使用PendingIntent.FLAG_IMMUTABLE for Android API 31+ 以符合安全要求。
为推送有效载荷配置 SDK
在 SDK 配置中添加推送通知有效载荷选择器,以指定 Singular 链接在通知数据结构中的位置。
val pushSelectors = arrayOf(
arrayOf("sng_link"),
arrayOf("rootObj", "nestedObj", "anotherNested", "singularLink")
)
val config = SingularConfig("SDK_KEY", "SDK_SECRET")
.withPushNotificationPayload(intent, pushSelectors)
Singular.init(applicationContext, config)
String[][] pushSelectors = {
{"sng_link"},
{"rootObj", "nestedObj", "anotherNested", "singularLink"}
};
SingularConfig config = new SingularConfig("SDK_KEY", "SDK_SECRET")
.withPushNotificationPayload(getIntent(), pushSelectors);
Singular.init(getApplicationContext(), config);
选择器配置:
-
简单密钥:对有效载荷中的顶级键使用
arrayOf("sng_link") -
嵌套键:使用
arrayOf("rootObj", "nestedObj", "key")遍历嵌套 JSON 结构 - 多路径:定义多个选择器数组,以检查奇异链接的不同可能位置
验证指南
验证启动会话中的有效载荷
通过检查启动会话 API 调用,确认推送通知链接已正确传递给 Singular。
当用户点击通知时,Singular SDK 会在开始会话请求的singular_link 参数下包含推送通知有效载荷。
启动会话请求示例:
https://sdk-api-v1.singular.net/api/v1/start?
a=<SDK-Key>
&singular_link=https://singularassist2.sng.link/C4nw9/r1m0?_dl=singular://test&_smtype=3
&i=net.singular.singularsampleapp
&s=1740905574084
&sdk=Singular/v12.6.2
替代验证:使用 Singular SDK 控制台验证推送通知跟踪。检查Deeplink URL字段,确认跟踪链接已正确捕获。
高级配置
ESP 域配置
如果您在电子邮件服务提供商(ESP)或其他第三方域中封装 Singular 链接,请配置外部域。
val config = SingularConfig("SDK_KEY", "SDK_SECRET")
.withESPDomains(listOf("sl.esp.link", "custom.domain.com"))
SingularConfig config = new SingularConfig("SDK_KEY", "SDK_SECRET")
.withESPDomains(Arrays.asList("sl.esp.link", "custom.domain.com"));
安全提示:默认情况下,只允许在 "Singular 管理链接 "页面中预定义sng.link域。 如果使用封装链接,请明确配置 ESP 域。
动态深度链接路由
通过配置一个具有动态重定向重写功能的 Singular 跟踪链接,从单个通知中实现多个深度链接目的地。
用例示例:具有多个操作选项的突发新闻通知
-
阅读最新新闻:
newsapp://article?id=12345 -
热门话题
newsapp://trending -
体育:
newsapp://sports
与其创建多个跟踪链接,不如使用一个单一链接,并根据用户选择动态覆盖重定向。有关实施详情,请参阅在奇异跟踪链接中覆盖重定向。
重要注意事项
实施注意事项
-
无回调处理程序:与
withSingularLink不同,推送通知功能不提供有效负载回调。请实施您自己的深度链接逻辑,将用户引导至您应用中的特定内容。 -
归属流:当用户点击通知时,Singular 会检索有效载荷并将其包含在由
Singular.init()触发的启动会话事件中。后台会处理这些数据,对推送通知接触点进行归属,并注册重新参与跟踪 -
域限制:默认情况下,只允许使用 "管理链接 "页面上的 Singular 链接域 (
sng.link)。请使用withESPDomains()为封装链接明确配置 ESP 域。
成功:通过这些步骤,您的应用程序现在可以通过 Singular 跟踪推送通知互动,从而提高活动绩效洞察力,并确保准确的再参与归因。