Hello 👋 How can we help?

Enable Push Notifications (optional)

Written by Konstantine
Updated 1 month 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 came from HelpCrunch server (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. Although, HelpCrunch Mobile SDK does not necessary require the implementation of push notifications and end users of your application will still be able to receive messages, but in this case, the end users will discover that new messages arrived only after opening HelpCrunch page.

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

Add Code for a Push Enabled iOS Applications

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

ObjC:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ...
  [HelpCrunch initForOrganization:@"YOUR_HELPCRUNCH_SUBDOMAIN" withAttributes:@{
    HC_ApplicationIdAttributeName:@"YOUR_APP_ID",
    HC_ApplicationSecretAttributeName:@"YOUR_APP_SECRET"
  }];
  
  [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 {
    ...
    HelpCrunch.initForOrganization("YOUR_HELPCRUNCH_SUBDOMAIN",
 withAttributes:[
       HC_ApplicationIdAttributeName:"YOUR_APP_ID",
       HC_ApplicationSecretAttributeName:"YOUR_APP_SECRET"
    ]) { (succeeded, error) in  }
    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 this 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 is 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. To do so, we can implement the [application:didReceiveRemoteNotification] method in the app delegate. In our case, 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 {
  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
    }
}

You can disable a default alert and implement your own alert. For disabling a default alert:

ObjC:
[HelpCrunch useDefaultAlertForRemoteNotification:NO]; // Defaults to YES
Swift:
HelpCrunch.useDefaultAlert(forRemoteNotification: false) // Defaults to YES

Did this answer your question?