Hello! 👋 How can we help?

Enable push notifications for iOS (optional) (iOS13 and higher)

Push notifications are great for real-time messaging and informing your app users that there is a new message for them.
Written by Andrew
Updated 3 weeks ago

We believe that push notifications are a great way to add real-time messaging to your application and inform your app users that there is a new message from HelpCrunch (i.e. from your admin dashboard). It allows you to stay in touch with your users even when the application is not in the foreground.

The HelpCrunch Mobile SDK does not necessarily require implementing push notifications.

Without push notifications, your app users will still be able to receive new messages, but they will only discover them after opening the HelpCrunch window.

When implementing push notifications for your app, remember that push notifications are not available in the iOS Simulator. To start developing this feature, you need an iOS device as well as the Apple Developer license.

Enabling push notifications

This manual is for those of you, who are using iOS 13 as minimum version. If you need to support earlier versions, you should also implement code from this article

To register your current device for push notifications, add the following code in the app delegate’s -application:didFinishLaunchingWithOptions: method:

ObjC:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
    // After HelpcrunchSDK init method
    [HelpCrunch registerForRemoteNotifications];
...
    return YES;
}

Swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
    // After HelpcrunchSDK init method
    HelpCrunch.registerForRemoteNotifications()
...
    return true
}

If the registration is successful, the callback method -application:didRegisterForRemoteNotificationsWithDeviceToken:
in the application delegate will be executed. We will need to implement this method and use it to inform HelpCrunch about a new device.

Important! If you use Firebase SDK, please, check this section.

ObjC:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [HelpCrunch setDeviceToken:deviceToken];
}

Swift:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    HelpCrunch.setDeviceToken(deviceToken)
}

UNUserNotificationCenterDelegate implementation

In order to process notifications, we need to implement UNUserNotificationCenterDelegate. HelpCrunch SDK uses its own delegate implementation, so you don't need to worry about it. But if you want to handle it by yourself, we got you covered:

First, disable HelpCrunch SDK's use of delegate, by utilizing  disablePushNotificationDelegate method on app launch in didFinishLaunchingWithOptions in AppDelegate:

ObjC:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
    [HelpCrunch disablePushNotificationDelegate];
...
    return YES;
}

Swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
    HelpCrunch.disablePushNotificationDelegate()
...
    return true
}

Finally, there is how we handle HelpCrunch SDK push notifications. If you have already implemented your own UNUserNotificationCenterDelegate class, it will help you adjust your code.

ObjC:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

    if (![HelpCrunch userNotificationCenterShouldPresentNotification:notification]) {
        completionHandler(UNNotificationPresentationOptionNone);
    } else {
        if (@available(iOS 14.0, *)) {
            completionHandler(UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner | UNNotificationPresentationOptionSound);
        } else {
            completionHandler(UNNotificationPresentationOptionAlert);
        };
    }
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    [HelpCrunch didReceiveRemoteNotification:response.notification.request.content.userInfo];

    completionHandler();
}

Swift:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            
    if HelpCrunch.userNotificationCenterShouldPresent(notification) {
        if #available(iOS 14.0, *) {
            completionHandler([.list, .badge, .sound])
        } else {
            completionHandler([.alert])
        }
    } else {
        completionHandler([])
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    HelpCrunch.didReceiveRemoteNotification(response.notification.request.content.userInfo)
    completionHandler()
}

Firebase SDK

Everyone's favourite SDK from Google uses method swizzling to get and map device token, so you won't be able to get it in the expected AppDelegate's  didRegisterForRemoteNotificationsWithDeviceToken method. In order to make our push notifications work, we need to disable method swizzling and apply APNs token to Firebase SDK by yourself. And here how you can do it:

Just add flag FirebaseAppDelegateProxyEnabled to your Info.plist and set it to NO. After that, make your didRegisterForRemoteNotificationsWithDeviceToken look like this:

ObjC: 

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [HelpCrunch setDeviceToken:deviceToken];
    [FIRMessaging messaging].APNSToken = deviceToken;
}

Swift:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    HelpCrunch.setDeviceToken(deviceToken)
    Messaging.messaging().apnsToken = deviceToken
}

Did this answer your question?