DEV Community

Khoa Pham
Khoa Pham

Posted on

Push Notification in iOS with Firebase

Original post https://github.com/onmyway133/blog/issues/64

Note: This applies to Firebase 4.0.4

Preparing push notification certificate

Go to Member Center -> Certificates -> Production

Certificate

You can now use 1 certificate for both sandbox and production environment
push

Auth Key

Configure push notification

  • Go to Firebase Console -> Settings -> Project Settings -> Cloud Messaging -> iOS app configuration
    • If you use certificate, use just 1 Apple Push Notification service SSL for both fields
    • If you use Authenticate Key, fill in APNS auth key

firebase

Adding pod

In your Podfile, declare

pod 'Firebase/Core'
pod 'Firebase/Messaging'
Enter fullscreen mode Exit fullscreen mode

Disabling app delegate swizzling

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
Enter fullscreen mode Exit fullscreen mode

Read more on Messaging.messaging().apnsToken

This property is used to set the APNS Token received by the application delegate.
FIRMessaging uses method swizzling to ensure the APNS token is set automatically. However, if you have disabled swizzling by setting FirebaseAppDelegateProxyEnabled to NO in your app's Info.plist, you should manually set the APNS token in your application delegate's -application:didRegisterForRemoteNotificationsWithDeviceToken: method.
If you would like to set the type of the APNS token, rather than relying on automatic detection, see: -setAPNSToken:type:.

Configuring Firebase

You can and should configure Firebase in code

import Firebase

let options = FirebaseOptions(googleAppID: "", gcmSenderID: "")
options.bundleID = ""
options.apiKey = ""
options.projectID = ""
options.clientID = ""
FirebaseApp.configure(options: options)
Enter fullscreen mode Exit fullscreen mode

Handling device token

import Firebase

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken
}

Enter fullscreen mode Exit fullscreen mode

Getting FCM token

Retrieving FCM token

Read Access the registration token

By default, the FCM SDK generates a registration token for the client app instance on initial startup of your app. Similar to the APNs device token, this token allows you to target notification messages to this particular instance of the app.

Messaging.messaging().fcmToken
Enter fullscreen mode Exit fullscreen mode

Observing for FCM token change

Read Monitor token generation

Messaging.messaging().delegate = self

// MARK: - MessagingDelegate

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  print(fcmToken)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
miqueasgutierrez profile image
miqueasgutierrez

Hello, very useful function, Also an easy way to integrate Push notifications is by integrating the INDIGITALL Service, it has been developed with the latest technology to guarantee maximum effectiveness and simplify the process of creating and sending Notifications with animated image, Segmented, Geolocated and many functions.
Here you can see the simple installation for your platform
docs.indigitall.com/
If you find it difficult to install, I can help you Free contact me miqueasmusic@gmail.com

Collapse
 
mrcflorian profile image
mrcflorian

Great tutorial, thanks for sharing! Unfortunately, lots of things have changed in the last 2 years, and some stuff doesn't work anymore. This Firebase Swift tutorial is a little more up to date.