要确保推送通知已完全开发,您需要确保跟踪部分已正确实施,因为并非每个推送通知都启用了跟踪。 要启用此功能,开发人员需要确定哪些投放启用了跟踪,Adobe Campaign Standard将发送一个名为的标记 _acsDeliveryTracking
具有两个值 日期 或 关. 应用程序开发人员应只对将变量设置为的投放发送跟踪请求 日期.
此变量不可用于在21.1版本之前设置的投放或使用自定义模板的投放。
推送跟踪分为三种类型:
推送展示 — 当推送通知已发送到设备并位于通知中心而根本没有被触碰时。 这被视为一次展示。 在大多数情况下,展示次数应与交付的展示次数大致相同。 它确保设备确实收到消息并将该信息中继回服务器。
推送点击 — 当推送通知已发送到设备并且用户已单击该设备时。 用户希望查看通知(该通知随后将变为推送打开跟踪)或关闭通知。
推送打开 — 当推送通知已发送到设备并且用户已单击该通知导致应用程序打开时。 这与推送点击类似,不同之处在于,如果取消通知,则不会触发推送打开。
要实施Campaign Standard跟踪,移动设备应用程序需要包含Adobe Experience Platform SDK。 这些SDK在以下位置提供: Adobe Experience Platform SDK文档.
要发送跟踪信息,需要发送三个变量。 其中两个是从Campaign Standard收到的数据的一部分,另一个是指示它是否为 展示, 单击 或 打开.
变量 | 值 |
---|---|
broadlogId | 来自数据的_mId |
deliveryId | 来自数据的_dId |
操作 | “1”表示打开,“2”表示单击,“7”表示展示 |
对于展示跟踪,调用时必须发送值“7”才能执行操作 collectMessageInfo()
或 trackAction()
函数。
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
....{Handle push messages}....
if (data.size() > 0) {
String deliveryId = data.get("_dId");
String messageId = data.get("_mId");
String acsDeliveryTracking = data.get("_acsDeliveryTracking");
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template
where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == null ) {
acsDeliveryTracking = "on";
}
HashMap<String, String> contextData = new HashMap<>();
if( deliveryId != null && messageId != null && acsDeliveryTracking.equals("on")) {
contextData.put("deliveryId", deliveryId);
contextData.put("broadlogId", messageId);
contextData.put("action", "7");
//If you are using ACPCore v1.4.0 or later, use the next line.
MobileCore.collectMessageInfo(contextData);
//Else comment out the above line and uncomment the line below
//MobileCore.trackAction("tracking", contextData) ;
}
}
}
对于点击跟踪,调用时必须发送值“2”才能执行操作 collectMessageInfo()
或 trackAction()
函数。
要跟踪点击情况,需要处理以下两种情况:
要解决此问题,您需要使用两个意图:一个用于单击通知,另一个用于关闭通知。
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
MyFirebaseMessagingService.java
private void sendNotification(Map<String, String> data) {
Intent openIntent = new Intent(this, CollectPIIActivity.class);
Intent dismissIntent = new Intent(this, NotificationDismissedReceiver.class);
openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//put the data map into the intent to track clickthroughs
Bundle pushData = new Bundle();
Set<String> keySet = data.keySet();
for (String key : keySet) {
pushData.putString(key, data.get(key));
}
openIntent.putExtras(pushData);
dissmissIntent.putExtras(pushData);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, openIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent onDismissPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, dismissIntent, 0);
//<BUILD NOTIFICATION using notification builder>
//Add both Intents to the notification
notificationBuilder.setContentIntent(pendingIntent);
notificationBuilder.setDeleteIntent(onDismissPendingIntent);
}
为了 BroadcastReceiver 要工作,您需要将它注册到 AndroidManifest.xml
<manifest>
<application>
<receiver android:name=".NotificationDismissedReceiver">
</receiver>
</application>
</manifest>
NotificationDismessedReceiver.java
public class NotificationDismissedReceiver extends BroadcastReceiver {
private static final String TAG = NotificationDismissedReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
String deliveryId = data.getString("_dId");
String messageId = data.getString("_mId");
String acsDeliveryTracking = data.get("_acsDeliveryTracking");
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template
where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == null ) {
acsDeliveryTracking = "on";
}
HashMap<String, Object> contextData = new HashMap<>();
//We only send the click tracking since the user dismissed the notification
if (deliveryId != null && messageId != null && acsDeliveryTracking.equals("on")) {
contextData.put("deliveryId", deliveryId);
contextData.put("broadlogId", messageId);
contextData.put("action", "2");
//If you are using ACPCore v1.4.0 or later, use the next line.
MobileCore.collectMessageInfo(contextData);
//Else comment out the above line and uncomment the line below
//MobileCore.trackAction("tracking", contextData);
}
}
}
您需要发送“1”和“2”,因为用户必须单击“通知”才能打开应用程序。 如果未通过推送通知启动/打开应用程序,则不会发生跟踪事件。
要跟踪未结项,您需要创建意图。 目的对象允许Android OS在完成某些操作后调用方法。 在这种情况下,单击通知可打开应用程序。
此代码基于点击展示跟踪的实施。 替换为 Intent 设置,此时您需要将跟踪信息发送回Adobe Campaign Standard。 在这种情况下,您需要设置 Open Intent 要打开应用程序中的某个视图,这将调用onResume方法,并将通知数据包含在 Intent Object.
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
@Override
protected void onResume() {
super.onResume();
handleTracking();
}
private void handleTracking() {
//Check to see if this view was opened based on a notification
Intent intent = getIntent();
Bundle data = intent.getExtras();
if (data != null) {
//This was opened based on the notification, you need to get the tracking that was passed on.
String deliveryId = data.getString("_dId");
String messageId = data.getString("_mId");
String acsDeliveryTracking = data.get("_acsDeliveryTracking");
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template
where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == null) {
acsDeliveryTracking = "on";
}
HashMap<String, String> contextData = new HashMap<>();
if (deliveryId != null && messageId != null && acsDeliveryTracking.equals("on")) {
contextData.put("deliveryId", deliveryId);
contextData.put("broadlogId", messageId);
contextData.put("action", "2");
//Send Click Tracking since the user did click on the notification
//If you are using ACPCore v1.4.0 or later, use the next line.
MobileCore.collectMessageInfo(contextData);
//Else comment out the above line and uncomment the line below
//MobileCore.trackAction("tracking", contextData);
//Send Open Tracking since the user opened the app
contextData.put("action", "1");
//If you are using ACPCore v1.4.0 or later, use the next line.
MobileCore.collectMessageInfo(contextData);
//Else comment out the above line and uncomment the line below
//MobileCore.trackAction("tracking", contextData);
}
}
}
对于展示跟踪,调用时必须发送值“7”才能执行操作 collectMessageInfo()
或 trackAction()
函数。
要了解iOS通知的工作方式,需要详细描述应用程序的三种状态:
为了还能 Impression 在应用程序处于后台时跟踪工作,我们需要发送 Content-Available 才能让应用程序知道必须完成跟踪。
如果某个应用程序关闭,则在重新启动该应用程序之前,Apple不会调用该应用程序。 这意味着您将无法知道何时在iOS上收到通知。 因此,iOS展示跟踪可能不准确,不应被视为可靠。
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
以下代码定位后台应用程序:
// In didReceiveRemoteNotification event handler in AppDelegate.m
//In order to handle push notification when only in background with content-available: 1
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//Check if the app is not in the foreground right now
if(UIApplication.shared.applicationState != .active) {
let deliveryId = userInfo["_dId"] as? String
let broadlogId = userInfo["_mId"] as? String
let acsDeliveryTracking = userInfo["_acsDeliveryTracking"] as? String
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == nil ) {
acsDeliveryTracking = "on";
}
if (deliveryId != nil && broadlogId != nil && acsDeliveryTracking?.caseInsensitiveCompare("on") == ComparisonResult.orderedSame) {
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"7"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"7"])
}
}
completionHandler(UIBackgroundFetchResult.noData)
}
以下代码定位前台应用程序:
// This will get called when the app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let deliveryId = userInfo["_dId"] as? String
let broadlogId = userInfo["_mId"] as? String
let acsDeliveryTracking = userInfo["_acsDeliveryTracking"] as? String
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == nil ) {
acsDeliveryTracking = "on";
}
if (deliveryId != nil && broadlogId != nil && acsDeliveryTracking?.caseInsensitiveCompare("on") == ComparisonResult.orderedSame) {
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"7"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"7"])
}
completionHandler([.alert,.sound])
}
对于点击跟踪,调用时必须发送值“2”才能执行操作 collectMessageInfo()
或 trackAction()
函数。
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
// AppDelegate.swift
...
import os.log
import UserNotifications
...
func registerForPushNotifications() {
let center = UNUserNotificationCenter.current()
center.delegate = notificationDelegate
//Here we are creating a new Category that allows us to handle Dismiss Actions
let defaultCategory = UNNotificationCategory(identifier: "DEFAULT", actions: [], intentIdentifiers: [], options: .customDismissAction)
//Add it to our array of Category, in this case we only have one
center.setNotificationCategories([defaultCategory])
center.requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
os_log("Permission granted: %{public}@", type:. debug, granted.description)
if error != nil {
return
}
if granted {
os_log("Notifications allowed", type: .debug)
}
else {
os_log("Notifications denied", type: .debug)
}
// 2. Attempt registration for remote notifications on the main thread
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
现在,当您发送推送通知时,您需要添加类别。 在本例中,我们将其称为“默认”。
然后处理 Dismiss 并发送您需要添加以下内容的跟踪信息:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
print("Dismiss Action")
let deliveryId = userInfo["_dId"] as? String
let broadlogId = userInfo["_mId"] as? String
let acsDeliveryTracking = userInfo["_acsDeliveryTracking"] as? String
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == nil ) {
acsDeliveryTracking = "on";
}
if (deliveryId != nil && broadlogId != nil && acsDeliveryTracking?.caseInsensitiveCompare("on") == ComparisonResult.orderedSame) {
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
}
default:
////MORE CODE
}
completionHandler()
}
您需要发送“1”和“2”,因为用户必须单击“通知”才能打开应用程序。 如果未通过推送通知启动/打开应用程序,则不会发生跟踪事件。
对于21.1版本之前创建的投放或使用自定义模板的投放,请参阅此 部分.
import Foundation
import UserNotifications
import UserNotificationsUI
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
// Called when user clicks the push notification or also called from willPresent()
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
os_log("App push data %{public}@, in userNotificationCenter:didReceive()", type: .debug, userInfo)
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
//This is to handle the Dismiss Action
let deliveryId = userInfo["_dId"] as? String
let broadlogId = userInfo["_mId"] as? String
let acsDeliveryTracking = userInfo["_acsDeliveryTracking"] as? String
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == nil ) {
acsDeliveryTracking = "on";
}
if (deliveryId != nil && broadlogId != nil && acsDeliveryTracking?.caseInsensitiveCompare("on") == ComparisonResult.orderedSame) {
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
}
default:
//This is to handle the tracking when the app opens
let deliveryId = userInfo["_dId"] as? String
let broadlogId = userInfo["_mId"] as? String
let acsDeliveryTracking = userInfo["_acsDeliveryTracking"] as? String
/*
This is to handle deliveries created before 21.1 release or deliveries with custom template where acsDeliveryTracking is not available.
*/
if( acsDeliveryTracking == nil ) {
acsDeliveryTracking = "on";
}
if (deliveryId != nil && broadlogId != nil && acsDeliveryTracking?.caseInsensitiveCompare("on") == ComparisonResult.orderedSame) {
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"2"])
//If you are using ACPCore v2.3.0 or later, use the next line.
ACPCore.collectMessageInfo(["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"1"])
//Else comment out the above line and uncomment the line below
//ACPCore.trackAction("tracking", data: ["deliveryId": deliveryId!, "broadlogId": broadlogId!, "action":"1"])
}
}
completionHandler()
}
}