Notifications

  • iOS
  • watchOS
  • tvOS
 

Foreword

Push notifications can trigger two application delegate methods depending on the context:

The delegate methods will only be triggered if the user touches the action button or message notification, and not the close/cancel button.

  1. application:didFinishLaunchingWithOptions:
    This method is called if the application has not yet been launched. The application will then be launched and the notification will be available in the “launchOptions” dictionary.
  2. application:didReceiveRemoteNotification:
    This method is called if the application has already been launched and is currently running or is in the background.

Tagging of notifications is done in the body of these methods.

 

Tagging examples

  • To measure a notification that was received when the application had not yet been launched, if the notification contains a reference to the campaign (in the ‘xto’ key)

    import UIKit
    import Tracker
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
                if let campaign = notification["xto"] as? String {
                    ATInternet.sharedInstance.defaultTracker.campaigns.add(campaign)
                }
                ATInternet.sharedInstance.defaultTracker.screens.add("PushNotification").sendView()
            }
            return true
        }
    }
    #import "AppDelegate.h"
    #import "SmartTracker/SmartTracker.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (notification) {
            [ATInternet.sharedInstance.defaultTracker.campaigns add:[notification objectForKey:@"xto"]];
        }
        
        [[ATInternet.sharedInstance.defaultTracker.screens add:@"PushNotification"] sendView];
        
        return YES;
    }
    
    @end
  • To measure a notification if the application is currently running or is in the background, if the notification contains a reference to the campaign (in the ‘xto’ key)

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        if let notification = userInfo[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
            if let campaign = notification["xto"] as? String {
                ATInternet.sharedInstance.defaultTracker.campaigns.add(campaign)
            }
            ATInternet.sharedInstance.defaultTracker.screens.add("PushNotification").sendView()
        }
    }
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        if ([userInfo objectForKey:@"xto"]) {
            [ATInternet.sharedInstance.defaultTracker.campaigns add:[userInfo objectForKey:@"xto"]];
        }
        [[ATInternet.sharedInstance.defaultTracker.screens add:@"PushNotification"] sendView];
    }

If you wish to simplify the tagging of your links without having to know the significance of each field, try the interface available in our marketplace. Please note that this interface has been created by our community and can be improved, so feel free to send us your feedback!

Last update: 18/05/2018