no-translate

Enable push notifications for iOS pre 13 version (optional)

Push notifications are great for real-time messaging and informing your app users that there is a new message for them.
Написано Andrew
Оновлено 1 рік тому

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's 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, you should remember that push notifications are not available in the iOS Simulator. To start developing this feature, you will need an iOS device, as well as the Apple Developer license.

Enabling push notifications

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];
if (![HelpCrunch didReceiveRemoteNotificationWithLaunchOptions:launchOptions]) {
// this push notification does not belong to HelpCrunch
}
...
return YES;
}

Swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
// After HelpcrunchSDK init method
HelpCrunch.registerForRemoteNotifications()
if !HelpCrunch.didReceiveRemoteNotification(launchOptions: launchOptions) {
// this push notification does not belong to HelpCrunch
}
}

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 the new device.

ObjC:

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

Swift:

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

When a push notification is received while the application is in the background, it will be displayed in the iOS Notification Center.

However, if the notification is received while the app is active, it is up to you how to handle it. We can implement the [application:didReceiveRemoteNotification:fetchCompletionHandler:] method in the app delegate. We will simply ask HelpCrunch to handle it for us. HelpCrunch will create a modal alert and display the push notification’s content.

ObjC:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
if (![HelpCrunch didReceiveRemoteNotification:userInfo]) {
// this push notification does not belong to HelpCrunch
}
}

Swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if !HelpCrunch.didReceiveRemoteNotification(userInfo) {
// this push notification does not belong to HelpCrunch
}
}

UNUserNotificationCenterDelegate implementation

Starting from iOS 10, in order to process notifications that arrive when your app is running in the foreground you need to implement UNUserNotificationCenterDelegate.

If you haven't yet, the HelpCrunch SDK can provide the built-in one so you won’t need to write additional code. Just set the shouldUsePushNotificationDelegate property of HCSConfiguration as true. Like this:

ObjC:

HCSConfiguration *configuration =
[HCSConfiguration configurationForOrganization:@"YOUR_HELPCRUNCH_SUBDOMAIN"
applicationId:@"YOUR_APP_ID"
applicationSecret:@"YOUR_APP_SECRET"];
configuration.shouldUsePushNotificationDelegate = true;
[HelpCrunch initWithConfiguration:configuration
user:nil
completion:^(NSError * _Nullable error) {
// Do something on SDK init completion
}];

Swift:

let configuration = HCSConfiguration(forOrganization: "YOUR_HELPCRUNCH_SUBDOMAIN",
applicationId: "YOUR_APP_ID",
applicationSecret: "YOUR_APP_SECRET")
configuration.shouldUsePushNotificationDelegate = true
HelpCrunch.initWith(configuration, user: nil) { (error) in
// Do something on SDK init completion
}

If you already have UNUserNotificationCenterDelegate implemented, you can add the following code snippet to it:

ObjC:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// ...
if ([HelpCrunch isVisible]) {
completionHandler(UNNotificationPresentationOptionNone);
} else {
// Your logic or just next line
completionHandler(UNNotificationPresentationOptionAlert);
}
}

Swift:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// ...
if HelpCrunch.isVisible() {
completionHandler([])
} else {
// Your logic or just next line
completionHandler(.alert)
}
}

Чи була наша стаття корисною?