Configure Push Notifications

Prerequisites

To get started, ensure the following prerequisites are met:

  • Register your application on Firebase. To register, refer to this guide.
  • Download the google-services.json file for Android and the GoogleService-Info.plist file for iOS from your Firebase project. Do not make any code changes suggested by Firebase at this stage.

Configuring push notifications for Android

Follow these steps to configure push notifications for your Android project:

  1. Add the google-services.json file you downloaded from Firebase to the android/app folder in your project.
  2. Add the following class path in dependencies classpath in the android/build.gradle file:
    'com.google.gms:google-services:4.4.2'
    
  3. In android/app/build.gradle, apply the following plugin at the top of the file, where other plugins are initialized:
    apply plugin: "com.google.gms.google-services"
    
  4. Add a colors.xml resource file in the android/app/src/main/res/values folder if one does not already exist. Add the following color resource:
    <color name="hydra_notification_color">#FF03DAC5</color>
    
  5. Add a small icon file named hydra_notification_small_icon to the android/app/src/main/res/drawable folder. This icon is used in the notification drawer. The file can be in XML, PNG, or JPG format.

Configuring push notifications for iOS

Follow these steps to configure push notifications for your iOS project:

  1. Add the GoogleService-Info.plist file you downloaded from Firebase to your iOS project using Xcode.
  2. Ensure you have added the Push Notification capability for your app identifier on the Apple Developer website.
  3. In your Xcode project settings for your application target, add the following capabilities under Signing & Capabilities:
    • Push Notification
    • Background Modes, with Background fetch and Remote notifications enabled.
  4. In HydraConfig.plist, set SUPPORTS_PUSH_NOTIFICATION to YES.

    📘

    Note

    It is mandatory to set up rich notifications for push notifications to function correctly on iOS. Refer to the instructions under Configure rich notifications for iOS for detailed steps.

Configuring rich notifications for iOS

Follow these steps to configure rich push notifications for your iOS project.

  1. Go to your project settings in Xcode and add a new target and select Notification Service Extension from the available templates. This adds a Notification Service Extension to your project.

  2. When configuring the new target, Xcode will append the ProductName of your main target to its bundle identifier to create the bundle identifier for this extension. You must add this extension bundle identifier on the Apple Developer website.

  1. Select the language as Objective-Cif your AppDelegate is Objective-C and Swift if your AppDelegate is Swift.

  2. Add the App Groups capability in Signing & Capabilities for both your main application target and the new extension target. You will need to create and add a group in both targets. The group name must follow the format group.<runner_target_bundle_id>.hydra, where <runner_target_bundle_id> is replaced with the actual bundle identifier of your main application target. This App group also needs to be added on the Apple Developer website for both app identifiers.

  3. In your ios/Podfile, add extension_pods within the target block for your new extension target, as shown here:

    target 'HydraRichNotification' do
     extension_pods
    end
    
  4. Install the pods again by opening your terminal, changing the current working directory to your project's ios folder, and running the command:

    pod install
    
  5. Replace the entire code NotificationService.mm(Objective-C) or NotificationService.swift (Swift) with the following code:

    #import "NotificationService.h"
    #import "HydraPushNotificationServiceExtension/HydraPushNotificationServiceExtension.h"
    @implementation NotificationService
    HydraRemoteNotificationService* service;
    -(id)init{
      self = [super init];
      service = [[HydraRemoteNotificationService alloc] init];
      return  self;
      
    }
    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
      [service didReceiveNotificationRequest:request withContentHandler:contentHandler];
    }
    - (void)serviceExtensionTimeWillExpire {
      [service serviceExtensionTimeWillExpire];
    }
    @end
    
    import UserNotifications
    import HydraPushNotificationServiceExtension
    class NotificationService: UNNotificationServiceExtension {
        let service:HydraRemoteNotificationService = .init()
        
        override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
            service.didReceive(request, withContentHandler: contentHandler)
        }
        
        override func serviceExtensionTimeWillExpire() {
            service.serviceExtensionTimeWillExpire()
        }
    }