Push Notification in iOS with Firebase
Tags: swift, push, notification, firebase
Published: 2017-08-09 09:12:04

---
title: Push Notification in iOS with Firebase
published: true
description: 
tags: swift, push, notification, 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](https://developer.apple.com/account/ios/certificate/distribution/create) -> `Certificates` -> `Production`

### Certificate
You can now use 1 certificate for both sandbox and production environment
![push](https://user-images.githubusercontent.com/2284279/29111626-5617e09c-7ceb-11e7-9f56-c1f9ceab3cd1.png)

### Auth Key
- If you like to use [Auth Key](https://developer.apple.com/account/ios/authkey/create), read [Provider Authentication Tokens](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW3)

## Configure push notification

- Go to [Firebase Console](https://console.firebase.google.com/) -> `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](https://user-images.githubusercontent.com/2284279/29111784-f0dfcbe4-7ceb-11e7-8aff-db1a50ea3ee5.png)

## Adding pod

In your `Podfile`, declare

```ruby
pod 'Firebase/Core'
pod 'Firebase/Messaging'
```

## Disabling app delegate swizzling

- Read [Method swizzling in Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/ios/client)
- You should say NO to swizzling. Do this in your `Info.plist`

```xml
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
```

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

```swift
import Firebase

let options = FirebaseOptions(googleAppID: "", gcmSenderID: "")
options.bundleID = ""
options.apiKey = ""
options.projectID = ""
options.clientID = ""
FirebaseApp.configure(options: options)
```
## Handling device token

```swift
import Firebase

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

```

## Getting FCM token

### Retrieving FCM token

Read [Access the registration token](https://firebase.google.com/docs/cloud-messaging/ios/first-message)

> 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.

```swift
Messaging.messaging().fcmToken
```

### Observing for FCM token change
Read [Monitor token generation](https://firebase.google.com/docs/cloud-messaging/ios/first-message#access_the_registration_token)

```swift
Messaging.messaging().delegate = self

// MARK: - MessagingDelegate

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  print(fcmToken)
}
```