Receive Push Notifications
- iOS
- Android
- React Native
-
Add your iOS app on Firebase Console:
Project Overview > Add App > iOS
-
Download
GoogleService-Info.plist
-
Add
GoogleService-Info.plist
using XCode by right clicking on project and selectAdd Files
, select your downloaded file and make sureCopy items if needed
is checked. -
In your
Podfile
add this:
pod 'DashX'
- 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")
}
}
- Disable swizzling for Firebase
Add the FirebaseAppDelegateProxyEnabled
flag in the app’s Info.plist file and set it to NO(boolean value)
Ensure the following is added to your app's manifest file:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" />
<application>
<service android:name="com.dashx.sdk.DashXFirebaseMessagingService" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Steps for iOS
-
Add your iOS app on Firebase Console:
Project Overview > Add App > iOS
-
Download
GoogleService-Info.plist
-
Add
GoogleService-Info.plist
using XCode by right clicking on project and selectAdd Files
, select your downloaded file and make sureCopy items if needed
is checked. -
In your
Podfile
add this:
pod 'DashX'
- 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)
print(actionIdentifier)
print("\n=================================\n")
}
}
- Disable swizzling for Firebase
Add the FirebaseAppDelegateProxyEnabled
flag in the app’s Info.plist file and set it to NO(boolean value)
Steps for Android
DashX requires Google Services installed in your app for Firebase to work:
- Add
google-services
plugin in your/android/build.gradle
buildscript {
dependencies {
// ... other dependencies
classpath 'com.google.gms:google-services:4.3.3'
}
}
- Add this line in your
/android/app/build.gradle
apply plugin: 'com.google.gms.google-services'
-
Add your Android app on Firebase Console:
Project Overview > Add App > Android
-
Download
google-services.json
from there. -
Add
google-services.json
at the following location:/android/app/google-services.json