Skip to main content

Receive Push Notifications

  1. Add your iOS app on Firebase Console: Project Overview > Add App > iOS

  2. Download GoogleService-Info.plist

  3. Add GoogleService-Info.plist using XCode by right clicking on project and select Add Files, select your downloaded file and make sure Copy items if needed is checked.

  4. In your Podfile add this:

pod 'DashX'
  1. Implement the DashXAppDelegate
import DashX
import FirebaseCore

import FirebaseMessaging

@main
class AppDelegate: DashXAppDelegate {

...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DashX.configure(
withPublicKey: '...',
)

// Initialize Firebase and FCM
FirebaseApp.configure()
Messaging.messaging().delegate = self

// Request permission for push notifications and handle the resulting authorization status
DashX.requestNotificationPermission { authorizationStatus in
switch authorizationStatus {
case .authorized:
// User has granted permission for push notifications
case .denied:
// User has denied permission for push notifications
case .notDetermined:
// User has not yet made a choice about push notifications
case .provisional:
// User has granted provisional permission for push notifications (iOS 12+)
@unknown default:
// Handle any future authorization status not accounted for in the switch statement
}
}

// This method registers the device token with DashX
DashX.subscribe()

return true
}

override func notificationDeliveredInForeground(message: [AnyHashable: Any]) -> UNNotificationPresentationOptions {
print("\n=== Notification Delivered In Foreground ===\n")
print(message)
print("\n=================================================\n")

// This is how you want to show your notification in the foreground
// You can pass "[]" to not show the notification to the user or
// handle this with your own custom styles
return [.sound, .alert, .badge]
}

override func notificationClicked(message: [AnyHashable: Any], actionIdentifier: String) {
print("\n=== Notification Clicked ===\n")
print(message)

// Get the required data from the notification message
let userId = message["USER_ID"] as! String

// Perform the task associated with the action.
switch actionIdentifier {
case "ACCEPT_ACTION": APIManager.accept(userId: userId)
case "DECLINE_ACTION": APIManager.decline(userId: userId)
// handle other actions
default: break
}

print("\n=================================\n")
}
}
  1. Disable swizzling for Firebase

Add the FirebaseAppDelegateProxyEnabled flag in the app’s Info.plist file and set it to NO(boolean value)