DEV Community

Dolapo Olakanmi
Dolapo Olakanmi

Posted on

Using .p12 Certificates for APNs Authorization in FCM Flutter Integration

Okay, I know using .p12 for APNs authentication is not recommended but hear me out. You might just need it which was the case for me. So, why not?

Ideally, you should use .p8 — recommended by Apple—for anything APNs Authorization on whatever push notification platforms you are using.

To send push notifications to iOS apps, an authenticated connection to Apple Push Notifications services (APNs) is required. You can authenticate using a .p8 key which is token-based key or a .p12 file which is certificate-based method but one is required.

If for some reason you don’t have a .p8 auth key and can not revoke for whatever reason, you can use the .p12 file which needs to be renewed yearly. We will see how to create this file for a project.

Recommended: It is recommended to use .p8 auth key if you can please use that.

To do this, make sure you have the following:

  • An iOS mobile app
  • A paid Apple Developer Account with admin Access.
  • An FCM account with owner role
  • A Mac with Xcode 14+
  • A Bundle ID for your app target as set in Xcode.
  • An Xcode project with Push Notification capability enabled

Step 1:

  • Generate a push certificate
  • You first need to create a certificate signing request .certSigningRequest file (csr) on macOS.
  • To do this go to Application > Utilities > Keychain Access.
  • Then launch the Certificate Assistant.
  • From the Menu bar click KeyChain Access > Certificate > Request a Certificate from a certificate Authority
  • The image is provided below:

  • You should see a dialog, Enter the necessary information.
  • You can leave out CA Email Address blank
  • Then select save to disk.
  • Click the Continue button and save to preferred location.

Step 2:

  • Enable push capabilities for the app.
  • By going to the identifier section of the Apple Developer portal.
  • Locate and select your App ID from the list.
  • Enable push Notification

Step 3:

  • Create a push certificate.
  • Go to the Certificate section of the Apple Developer portal.
  • Click the + sign to create a new certificate.

  • Under the Services, select Apple Push Notification service SSL (Sandbox & Production)
  • Then click Continue.

On the next screen select your APP ID from the list and click Continue.

Upload the previous generated .certSigningRequest. click Continue.
On the next screen click Download.
You should have a .cer file

Step 4:

  • Create a private key and export the .p12 certificate
  • Double click the downloaded .cer file to import it into KeyChain Access.
  • In Keychain Access, navigate to:
  • Keychains > Login
  • Category > My Certificates
  • Locate the certificate named Apple Push Services.
  • Right-click the certificate and select Export.
  • Choose a location to save the file, and select the file format as .p12. When prompted, you can set a password for the .p12 file (this password is required when uploading to services like FCM).

You should have your .p12 file ready for upload to any push notification service. The next step is integrating FCM to your flutter project.
You follow this steps:

Step 5:

I’ll assume you already know how to set up a target and its bundle identifier in Xcode, as well as configure flavors and package names in Android Studio. Once that’s in place, you can use the following command to configure each environment. In this example, we’re setting up the dev environment.

  • Configure flutterfire: You can copy the command below and paste on the terminal with the your appropriate value.
flutterfire config \
  --project=project-id \
  --out=lib/firebase_options_dev.dart \
  --ios-bundle-id=com.bundle.id.dev \
  --ios-out=ios/flavors/dev/GoogleService-Info.plist \
  --android-package-name=com.package.name.dev \
  --android-out=android/app/src/dev/google-services.json
Enter fullscreen mode Exit fullscreen mode
  • Select the platform your application supports:

  • Depending on what you want you can either select the Build Configuration or Targets. In this case, I selected the Target.

  • Select the specific target:

After this, the appropriate files will be created in your flutter lib folder, ios flavor folder and android src folder.

Reference:

Thanks for reading.

Top comments (0)