Customize mobile app messages for iOS apps

Receive mobile app messages from outside Acoustic Campaign

You can use the SDK to receive notifications that did not originate from the MCE server. Push notifications can be from different sources. For example, they can be from your marketing team or from a third-party to notify file updates.

All these push sources use the same push token when they register. The same push token makes it easy to integrate other push sources with the app.

📘

Note:

SDKs that integrate by method swizzling can be problematic. Such integrations are not addressed here.

  1. Create a new NotificationDelegate class in the main target. For the sample app, create the class in the SwiftSample.
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
    static let shared = NotificationDelegate()

    // This method processes the notification tap
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        guard response.notification.request.content.userInfo["notification-action"] == nil else {
            MCENotificationDelegate.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
            return
        }
        // handle other types of notifications here
    }

    // This method is used to determine if the notification should be shown to the user when the app is running
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        guard notification.request.content.userInfo["notification-action"] == nil else {
            MCENotificationDelegate.shared.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
            return
        }

        // handle other types of notifications here, typically via
        completionHandler([.alert, .sound, .badge])
    }

    // This method is used to open the settings screen in the app for push notification preferences, only implement if you provide one
    func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
        // show settings screen if available
    }
}
  1. Configure the NotificationDelegate class. The configuration passes control to other appropriate classes when non-Acoustic messages are received.
  2. Update your app delegate to use the NotificationDelegate class you added. You can update the UNUserNotificationCenter.current().delegate when the app finishes launching. In the sample, you must update the SwiftSample.AppDelegate similarly.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    UNUserNotificationCenter.current().delegate = NotificationDelegate.shared
}

Detect incoming mobile app messages

Use notification controls in Mobile App Messaging to detect incoming mobile app messages. To detect incoming mobile app messages:

For iOS 10 (and later):

MCESdk.sharedInstance.presentNotification = ^BOOL(NSDictionary*){
 NSLog(@"Checking if should present notification!");
 
 // return FALSE if you don't want the notification to show to the user when the app is active
 return TRUE;
 };

For iOS 9 (and earlier):

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

📘

Note:

iOS only calls this method when the "content-available" flag is used in the push payload.

Cancel mobile app messages

In iOS 10 (and later), you can cancel mobile app messages when the app is open. When you cancel the message, the incoming mobile app message is not displayed. Then, you can display your mobile app message. To cancel incoming mobile app messages when the app is open:

For iOS 10 (and later)
The following method should return FALSE. If TRUE is returned, the incoming mobile app message is displayed.

MCESdk.sharedInstance.presentNotification = ^BOOL(NSDictionary*){
 NSLog(@"Checking if should present notification!");
 
 // return FALSE if you don't want the notification to show to the user when the app is active
 return FALSE;
 };

For iOS 9 (and earlier)
You cannot cancel incoming mobile app messages.

Add number badge to your application

iOS Applications can have a number badge on their home screen icon. You can add the number badge with the UIApplication class's setApplicationIconBadgeNumber method.

The SDK sends an NSNotification with the name "InboxCountUpdate" in the following scenarios:

  • when the number of inbox messages changes or
  • when the number of unread messages changes.

You can use this method to keep the application badge count up to date.
The sample project includes examples that show how to use the notification to update the application badge count.

-(void)inboxUpdate {
    int unreadCount = [[MCEInboxDatabase sharedInstance] unreadMessageCount];
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication.sharedApplication setApplicationIconBadgeNumber: unreadCount];
    });
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self inboxUpdate];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inboxUpdate) name: InboxCountUpdate object:nil];
    ...
}
@objc func inboxUpdate() {
        DispatchQueue.main.async {
            UIApplication.shared.applicationIconBadgeNumber = Int(MCEInboxDatabase.shared.unreadMessageCount())
        }
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
    {
        inboxUpdate()
        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.inboxUpdate), name:
            MCENotificationName.InboxCountUpdate.rawValue, object: nil)
            ....
    }