3.2.1. Sending Push Notifications

Setup Guide

🚧

Before you begin, make sure that you’ve completed the following steps:

šŸ‘

ByĀ the end ofĀ this guide:

Maestra will beĀ able toĀ send aĀ push notification and itĀ will beĀ displayed onĀ aĀ device.

ToĀ check that your push notifications are being sent, please refer toĀ the Maestra mobile push notification control guide.

AĀ push notification isĀ aĀ message that pops upĀ onĀ aĀ mobile device with aĀ title, text body, and aĀ URL the user isĀ taken toĀ when they click the notification.

Unlike rich push notifications, basic ones doĀ not display buttons orĀ pictures.

1. Enable push notifications inĀ app settings

  1. Open the project settings.
  2. Tick what you will set upĀ onĀ the list ofĀ Targets.
  3. GoĀ toĀ the Signing &Ā Capabilities tab.
  4. Click Add and tick Push Notifications and Background Modes.
  5. OnĀ the Background Modes pane, tick:
    • Background fetch;
    • Remote notifications, and
    • Background processing.

Deliver the keys toĀ your Maestra Forward Deployed Marketer toĀ connect toĀ Apple Push Notification service and add the keys yourself.

2. Request permission toĀ display notifications

Open AppDelegate.swift toĀ find and implement the registerForRemoteNotifications function. Call Mindbox.shared.notificationsRequestAuthorization from this function.

ToĀ call the function, use the didFinishLaunchingWithOptions method:

//
//  AppDelegate.swift
//  Sample
//
//  Created by Peter Nikitin on 06.04.2022.
//

import Foundation
import Mindbox
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
        
    ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {  
      // Call the notification registration method
      registerForRemoteNotifications()
    
        ...

    }

  //    MARK: registerForRemoteNotifications
  //    Function toĀ request permission toĀ display notifications. InĀ the Completion block, send the permission status toĀ Mindbox’s SDK
  func registerForRemoteNotifications() {
    UNUserNotificationCenter.current().delegate = self
    DispatchQueue.main.async {
      UIApplication.shared.registerForRemoteNotifications()
      UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge]) { granted, error in
        print("Permission granted: \(granted)")
        if let error = error {
          print("NotificationsRequestAuthorization failed with error: \(error.localizedDescription)")
        }
        Mindbox.shared.notificationsRequestAuthorization(granted: granted)
      }
    }
  }
      
    ...

}

3. Implement standard notification display

Open AppDelegate.swift toĀ find and implement the userNotificationCenter.willPresent function for received notifications toĀ beĀ displayed onĀ aĀ screen.

//
//  AppDelegate.swift
//  Sample
//
//  Created byĀ Peter Nikitin onĀ 06.04.2022.
//

import Foundation
import Mindbox
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
        
    ...

    func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  ) {
      completionHandler([.alert, .badge, .sound])
  }
      
    ...

}

Pass the APNs token toĀ the SDK

Open AppDelegate.swift toĀ find (orĀ create) the didRegisterForRemoteNotificationsWithDeviceToken method. Call Mindbox.shared.apnsTokenUpdate(deviceToken: deviceToken) from this method.

//
//  AppDelegate.swift
//  Sample
//
//  Created byĀ Peter Nikitin onĀ 06.04.2022.
//

import Foundation
import Mindbox
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
        
                ...

   func application(
        _ application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Mindbox.shared.apnsTokenUpdate(deviceToken: deviceToken)
  }
        
                ...

}
šŸ‘

Check your results:

You should now beĀ able toĀ send aĀ push notification via Maestra that isĀ displayed onĀ aĀ mobile device screen.

ToĀ check that your push notifications are being sent, please refer toĀ Maestra Mobile Push Control Guide.