For the complete documentation index, see llms.txt. This page is also available as Markdown.

iOS Push Message Integration

Can be used alongside other push solutions

To use alongside another push solution, you must disable the Swizzling option of that solution.

After disabling Swizzling, configure push notification handling manually by following that solution's guide.

1

Configure APNs

To use push messages in an iOS app, you need to configure the integration between the Hackle Workspace and APNs.

For details, refer to Apple Push Notification Service Configuration.

2

Add PushNotification Capability to App

Open the project by opening /ios/Runner.xcworkspace in Xcode, then click + Capability in the Signing & Capabilities tab of the project settings as shown below.

Add Push Notifications and Background Modes.

Then enable Remote notifications under Background Modes.

3

Configure AppDelegate

You must configure AppDelegate to enable push token collection, push message display, and push click handling.

Modifying AppDelegate is required for push message integration.

Open the project by opening /ios/Runner.xcworkspace in Xcode, then open the AppDelegate file.

4

Collect Push Token

Add the setPushToken method to AppDelegate as shown below.

import Flutter
import Hackle
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
      ...
      // Add inside existing implemented code
      // Request push permission in iOS app
      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
      UNUserNotificationCenter.current().requestAuthorization(
          options: authOptions,
          completionHandler: { _, _ in }
      )
      NUserNotificationCenter.current().delegate = self
      application.registerForRemoteNotifications()
      ...
    }

    override func application(
      _ application: UIApplication,
      didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
      // Send APNs push token to Hackle server
      Hackle.setPushToken(deviceToken)
    }
}
5

Display Push Message

In the background, pushes are displayed automatically without any code implementation.

Add the userNotificationCenter method to display foreground push messages.

import Flutter
import Hackle
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
  ...
  // Add inside existing implemented code

  // Foreground push message
  override func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions
  ) -> Void) {

    if Hackle.userNotificationCenter(
      center: center, willPresent: notification, withCompletionHandler: completionHandler
    ) {
      // Succefully processed notification
      // Automatically consumed completion handler
      return
    } else {
      // Received not hackle notification or error
      print("Do something")

      if #available(iOS 14.0, *) {
        completionHandler([.list, .banner])
      } else {
        completionHandler([.alert])
      }
    }
  }
}

If the push was not sent by Hackle, false is returned.

6

Handle Push Click

Add the handleNotification method to handle push clicks.

import Flutter
import Hackle
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
  ...
  // Add inside existing implemented code

  // push click
  override public func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {

    if let _ = Hackle.handleNotification(response: response) {
      // process hackle notification
    } else {
      // not hackle notification or error
      print("do something")
    }

    // handleNotification does not call completionHandler,
    // so you must call completionHandler regardless of whether it is a Hackle push.
    completionHandler()
  }
}

The push click function processes in the following order:

  1. Check if the push was sent by Hackle

  2. Send the push click event to the Hackle server

  3. (If it is a deep link push) Handle the deep link

If the push was not sent by Hackle, nil is returned.

7

Test Push Message

Check Token

Test

8

Receive Push Message

On iOS, whether push messages can be received depends on the build environment.

Even when the APNs Key Environment is set to Sandbox & Production, the scope for receiving push messages varies by Hackle environment and app build environment as follows.

Hackle Environment
APNs Environment
Build Environment

Development Environment, Development/Production test push

Sandbox

Direct run from Xcode, Development provisioning

Production Environment

Production

TestFlight, Ad Hoc, App Store distribution

Hackle push messages support deep link navigation when tapped.

When the corresponding activity is opened via a push message, you can retrieve the deep link information using the method below.

For more details about Flutter deep links, refer to the Flutter Deep Linking Guide.

Last updated