How To Register An App For Push Notifications Ios
Push button Notifications is a loud and powerful way for our apps to appoint with our users. We engage with our users by letting them see values with their very owns eyes. Users see values. Users engage. We can engage our users by letting them know near something of import. Perchance their favorite sport team is near to compete. A flash sale on watermelons is on for thirty minutes. Or there is an important meet upward in the desert later this weekend. These are some scenarios when our users may want to be notified.
One manner to implement push notifications for iOS apps is through Firebase Notifications.
In this tutorial, we will dive into Firebase Notifications. We volition run into how we can implement this feature into our Xcode project i step at a time. This tutorial is split up into 7 fine looking sections:
- Apple Developer Account Configuration
- Generating a CSR file
- Uploading your CSF file
- Preparing the APNs Document
- Configuring Firebase for Push Notifications
- Building the Firebase Notification Demo App
- Testing Push Notifications
I will try to be equally clear and concise equally possible. Promise you enjoy the commodity and let'due south get started!
Configuring Your Apple Developer Account
The start stride is to accept a paid Apple programmer account. Aye, yous demand to enroll into the Apple tree Developer Program ($99 per year) to unlock the push notifications capability.
Assuming yous already got a paid developer account, go alee and login to your Apple Developer account. One time logged in, you will exist re-directed to the Apple tree Programmer homepage. From in that location, yous should see "Account" at the top navigation bar. Click that option.
Now you should be inside of your Apple Developer account.
Now look to the left side bar. The third row should say "Certificates, IDs & Profiles." Select that pick.
At present you lot are in the "Certificates, Identifiers & Profiles" page.
Look to left side bar and there should exist a section called "Identifiers." Under that section, in that location is a link that says "App IDs." Printing that.
You lot should see all your iOS App IDs.
Now at the superlative right, you should see a +
button. Printing that. After that, you should be at this stage:
Nosotros at present need to fill out the followings:
- App ID Description — Proper name. Here, yous should put your app'southward name (due east.yard. Firebase Notification Demo)
- App ID Suffix — Explicit App ID — Package ID. Here, you need to select a unique bundle identifier for your app (e.g. com.appcoda.firebasenotificationsdemo). Please make sure you use your own bundle ID instead of using mine.
Then under App Services, tick "Push button Notifications." Printing continue.
After that, y'all will be redirected to a "Ostend your App ID" page. Press register.
Now we are back to our "iOS App IDs" page. Look for the App ID you lot just created. Press on it and you should see a a drib downwardly of Application Services.
Whorl down until you reach the cease of the drop down and you should see an "Edit" push button. Press that.
The "iOS App ID Settings" page will show upward.
Whorl all the way down until you see "Push Notifications."
It is time for us to create a "Customer SSL Certificate." This will allow our notification server (Firebase) to connect to the Apple Button Notification Service. Under Development SSL Certificate, press on the "Create Document…" push button.
Now we will run into this.
To generate a certificate, we would need a Certificate Signing Request (CSR) file from our Mac. We will become back to this folio later, just at present we need the CSR file.
Generating a CSR file
To generate a CSR file, press cmd + infinite and do a spotlight search for "Keychain Access." Open Keychain Admission, and become upward to the carte du jour to select Keychain Access > Document Banana > Asking a Certificate From a Document Authority…
A "Certificate Assistant" pop up should appear.
Fill in your email accost and name. Cull "Saved to disk" and press Continue. Then save your CSR somewhere on your difficult bulldoze.
Uploading Your CSR File
At present that we take our CSR generated, information technology is ready to become back to the "Add iOS Certificate" page.
Roll down. Press continue, and then click "Choose file…" Select the CSR file you just saved on your hard drive.
Side by side, click go on again. And so the web page should say "Your certificate is set."
At present yous tin can go alee and click on the blueish download push to download your document.
Preparing the APNs Certificate
Now that you have created the iOS certificate, we will then set the APNs (short for Apple Push Notifications) certificate, which will be used later in the Firebase configuration. Open up Finder and locate the certificate yous downloaded earlier.
Double click the certificate file (e.g. aps_development.cer) to add the certificate into Keychain Access.
Now open up Keychain Access. Under the "My Certificates" category, you lot should see the certificate yous just added. It should be called:
Apple Development IOS Push Services : < your . bundle . id > |
Click the aggrandize arrow to the left of the certificate'southward name to reveal the individual key pick. Correct click on the private central and press consign.
Once clicking Export, a pop upward will prompt yous to save your private key as a .p12
file. Go alee and click save.
Then enter a password if you lot want to protect your exported certificate. Click OK to confirm.
Crawly! We just completed the configuration and preparation. At present nosotros are ready to move onto Firebase! Allow's get started.
Configuring Firebase for Push Notifications
Commencement, head over to Firebase console. Sign in with your Google business relationship to enter the console. If you don't know how to switch to the console, there is a button at the very meridian right that says "Become to console." Go alee and click on that.
In one case you lot are at the console, click on the "CREATE A NEW PROJECT" push button.
I proper noun my projection "Firebase Notification Demo" but you're complimentary to use other names. Only click the "CREATE A NEW PROJECT" button to proceed.
After that, you are redirected to the project overview page. Now click the "Add together Firebase to your iOS" button. Enter your iOS bundle ID. And then click the "Add APP" push.
Follow the on-screen instruction to download the GoogleInfo.plist
file. We are going to need this file after.
Click "continue" to get to the next step. You volition run into instructions showing you how to add the Firebase SDK into our project. I volition walk you through how to configure the SDK later. For now, simply ignore and click "Continue" to go on.
Finally click "Finish" to complete the configuration. You should so encounter your iOS app in the Firebase overview folio.
Look for the setting icon at the top correct. Click the settings icon > Project settings.
Select the Cloud Messaging tab.
Gyre down, and click the "Upload Certificate" button.
Then a popular should announced to inquire you for your Development APNS certificate.
Click browse and choose your APNs certificate (i.east. .p12
file) that you prepared in the earlier section. If you configured the file with a password, enter certificate password, followed by clicking the Upload button.
Now y'all should see a Evolution APNs document file in the Deject Messaging setting.
For this demo, nosotros are going to use the Evolution APNs document. If you do make up one's mind to put your app onto the App Store, make certain that you take a Product APNs certificate file for Firebase as well.
Great! We finally finish all the configurations. It'south time to head back to Xcode to create the demo.
Edifice the Firebase Notification Demo App
Now fire up Xcode and create a new project using the Single View Application template. Proper name your Xcode project whatsoever you lot want.
Please note that your bundle ID should be identical to the one you set in your Apple Developer dashboard. If yous already forgot the ID, yous tin go back to App IDs under Identifiers and see what it is. In case you use a wrong ID, y'all volition not be able to receive whatsoever push button notifications.
Installing the Firebase SDK Using CocoaPods
Next, information technology is time for us to integrate the Firebase SDK into our Xcode projection. The simplest way to exercise that is by using CocoaPods. First, close the Xcode projection and and then openFinal. Change to the directory of your project and run the post-obit command to initialize your Podfile:
Open the Podfile, add together the following pods to your Podfile:
pod 'Firebase/Core' pod 'Firebase/Messaging' |
Save it. Your podfile should look something like this:
# Uncomment the next line to define a global platform for your projection # platform :ios, '9.0' target 'FirebaseNotificationDemo' exercise # Comment the next line if yous're not using Swift and don't want to use dynamic frameworks use_frameworks ! # Pods for FirebaseNotificationDemo pod 'Firebase/Cadre' pod 'Firebase/Messaging' cease |
Go back to your terminal and run the following command to install the pods:
CocoaPods will then download the SDK for you and create a .xcworkspace
file for you. Make sure you open that file in Xcode.
Adding GoogleService-Info.plist
With the SDK installed, it'southward time to add the GoogleService-Info.plist
file that you have downloaded before. Drag that file from Finder to your project folder.
Once it is done, yous should see the GoogleService-Info.plist
in your project folder.
Notation: After calculation the GoogleService-Info.plist file to your Xcode project, please check the package ID of your projection. Make certain the bundle ID is the same as you lot configured before. This is very important as your app will non receive push notifications if this packet ID is wrongly set.
Enabling Push Notifications
Cool! The side by side step is to enable the Push button Notifications capability of the demo project. Select the project in the project navigation, and so cull Target > Capabilities. Flip the switch of Push Notifications to ON
to enable it.
Initializing Push Notifications
It's time to write some code and test push notifications. Head over to AppDelegate.swift
and import the following modules:
import UserNotifications import Firebase import FirebaseInstanceID import FirebaseMessaging |
Adopt the following protocols in your App Consul class:
UNUserNotificationCenterDelegate , FIRMessagingDelegate |
Next, implement the applicationReceivedRemoteMessage
required method of the FIRMessagingDelegate
protocol like this:
// The callback to handle information message received via FCM for devices running iOS 10 or in a higher place. func applicationReceivedRemoteMessage ( _ remoteMessage : FIRMessagingRemoteMessage ) { print ( remoteMessage . appData ) } |
Now inside of your application(_:didFinishLaunchingWithOptions:)
method, add the following code:
1 ii 3 4 five 6 7 8 9 10 11 12 thirteen 14 15 xvi 17 18 | if #available ( iOS 10 . 0 , *) { // For iOS ten display notification (sent via APNS) UNUserNotificationCenter . current ( ) . delegate = self let authOptions : UNAuthorizationOptions = [ . alarm , . badge , . sound ] UNUserNotificationCenter . current ( ) . requestAuthorization ( options : authOptions , completionHandler : { _ , _ in } ) // For iOS 10 data message (sent via FCM FIRMessaging . messaging ( ) . remoteMessageDelegate = self } else { permit settings : UIUserNotificationSettings = UIUserNotificationSettings ( types : [ . warning , . bluecoat , . audio ] , categories : null ) awarding . registerUserNotificationSettings ( settings ) } application . registerForRemoteNotifications ( ) FIRApp . configure ( ) |
This sets up push notification settings based on your iOS version. The terminal line FIRApp.configure()
is to initialize and configure Firebase. The AppDelegate
class should look something like this:
Once yous have your App Consul prepare, you can run your Xcode project on your device. Make sure you lot tap Allow when your app asks you for notifications permission.
Great! You are skillful to become, and allow's try to send some notifications!
Testing Firebase Notifications
Alright, it's time to do some tests and endeavour to send some notifications. Go back to the Firebase Console, and then select your Firebase project. Look to the left side bar and you should see the Notifications choice. Click the button. Once yous encounter the Notification page, click the "SEND YOUR Showtime MESSAGE" button.
Later on so much work, it is about time that nosotros ship "the best push notification ever" from Firebase. Let's become ahead and do that.
Fill in the "Message text" field with a message you lot would similar your users to come across. And so select your app under the Target section.
Scroll down and press the "SEND MESSAGE" button.
A popular up will ask you to review your notification message. Printing the Send push button to send out your notification.
If yous lock your device, you should be able to encounter the notification you pushed on your lock screen. You can then swipe from left to right on the notification to open the app. If you lot are on your home screen, you will a notification message drop down from the pinnacle of your screen. Y'all can tap on the notification to open the app.
That's information technology!
Wrap Up
We have implemented Firebase Notifications into our Xcode project. We took it ane step at a time and made to the end. Congratulations for making information technology this far!
I hope you have enjoyed and learned something valuable from my article. If you have, please let me know by sharing this article with your friends.
Source: https://www.appcoda.com/firebase-push-notifications/
Posted by: whitehatian.blogspot.com
0 Response to "How To Register An App For Push Notifications Ios"
Post a Comment