Skip to content

Instantly share code, notes, and snippets.

@jagroop
Forked from rolinger/gist:d6500d65128db95f004041c2b636753a
Last active December 13, 2017 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagroop/059312146bacef16a77d7dcd27e232ca to your computer and use it in GitHub Desktop.
Save jagroop/059312146bacef16a77d7dcd27e232ca to your computer and use it in GitHub Desktop.
PHP => FCM Push notification tutorial for Android and iOS

Below is a full tutorial on how to setup and use Googles Firebase push notification API for both Android and iOS. It is based on this earlier implementation of Googles GCM method: https://gist.github.com/prime31/5675017 - FCM is the new method and GCM will eventually be retired.

THE BELOW METHOD IS THE NEWER FCM METHOD:

Register your app in the FCM Console: https://console.firebase.google.com (add project)

  1. Click on the newly added project, in the upper left menu is the "Overview" and Gear Settings.
  2. Click on the GEAR settings icon, and then on "Project Settings"
  3. In the main screen, click on "Cloud Messaging"
  4. Here you will find both your "Server Key" and "SenderID"
  5. The "Server Key" is what is used below as the API_ACCESS_KEY
  6. The "SenderID" is what your app (on the client phone) will need to generate a 'registration_ID'
  7. You now need to register the "senderID" in your developer play console of your app
  8. Go to: https://play.google.com, login to manage your app on the playstore
  9. Click on the specific app and in the left menu under "Development Tools" click on "Serivces & APIs"
  10. Under the Firebase Cloud Messaging (FCM) section, click "Link Sender ID", add your "server key" and click "link". After it links, your "senderID" will appear in the linked section.
  11. In your App, (I use Cordova => phonegap-plugin-push plugin), make a call the FCM server, passing the senderID
  12. The client App will receive the "registration_ID". - this is a unique ID that specifically registers the users phone to receive messages on behalf of your app.
  13. Your app will need to pass the 'registration_ID' to your server. In my case, my app passes it via an api call. But your app could simply pass a silent url call to your server to capture the users 'registration_ID' (ie: http://yourserver.com/passID.php?userID=jdoe&regID=$registration_ID).
  14. Ideally, you will want to store that registration_ID locally on your server (mysql).
  15. In the future, when you need to push a notification to that user, call up the stored registration_ID and pass it to the script below:

NOTE: Once your register your app in the FCM console and retrieve your Server Key and SenderID, it might take 12 to 24 hours for the IDs to propagate to the FCM messaging servers. I experienced this, minutes after registering my code worked but phone never received the messages...10 hours later...no changes to my code...phone suddenly started getting the messages. I assume it was a propagation issue.

REGISTERING YOUR APPLE APNs KEY in GOOGLE'S FCM CONSOLE:

  1. You will need to link the Apple version of your app to your FCM App project. On the Firebase project main page, click add "+ Add another App". Then "iOS App"
  2. After it is linked, in the same section as #4 & #5 above, you will see a section for "iOS Apps" and you should see your App name listed
  3. Here you can add your APNs Auth Key ....OR....add APNs Certificates - it is easier and recommend to do the Auth Key method.
  4. Login to your Apple Developer Account: https://developer.apple.com/
  5. Click on "Account" , then on "Certificates, IDs & Profiles"
  6. Make certain your on the "ALL" section in the left hand menu, the click the (+) sign in the top right of the main screen
  7. Under the "Production" section, select the "Apple Push Notification Authentication Key (Sandbox & Production) option.
  8. Click on continue
  9. You will be provided with an Auth Key AND a downloadable authkey file - you will need both. Download the file - but do NOT change the name of the file
  10. While here copy down your Team ID - click on "Account" again, then on "Membership" in the left hand menu
  11. Copy your "Team ID"
  12. Back in the FCM Console (#3), click on the "add/upload" button in the Apple APNs section next to your listed app.
  13. KeyID = #9 Auth Key, File = "#9 downloaded file", App ID Prefix = "#11 Team ID"
  14. Save/upload

You iOS app is now registered with Googles FCM, Google FCM can now send/relay messages to iPhones/iPads The only remaining step is to have your client iOS/iPhone App retrieve and send a proper registrationID to your server side script (below). In my case, the same Cordova phonegap-plugin-push plugin extracts the Apple registrationID too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment