Fireship · @fireship

Send Push Notifications in Angular with Firebase Cloud Messaging

Uploaded August 4, 2017 · Length 5:39

Send Push Notifications in Angular with Firebase Cloud Messaging

Summary

Send push notifications in Angular using Firebase Cloud Messaging with a service worker and cloud function.

  • firebase cloud messaging
  • angular push notifications
  • service worker
  • cloud function
  • firebase cloud functions

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Firebase Cloud Messaging allows you to

  2. 0:02

    send push notifications throughout

  3. 0:04

    multiple devices even after an app's

  4. 0:06

    been closed. It does this on the web by

  5. 0:08

    registering a service worker that

  6. 0:10

    listens for messages from Firebase in

  7. 0:12

    the background. As you can see here,

  8. 0:13

    we've completely closed our Angular app

  9. 0:15

    in the browser, but we still get this

  10. 0:17

    push notification on our operating

  11. 0:19

    system directly from Firebase. Many apps

  12. 0:22

    do this to notify their users of

  13. 0:24

    important information such as Slack,

  14. 0:26

    which will notify users when they have a

  15. 0:28

    new mention on a channel. Let's get

  16. 0:31

    started by building this worker into our

  17. 0:32

    Angular app. Create a new file in the

  18. 0:34

    source directory called Cloud Messaging

  19. 0:37

    SW.js.

  20. 0:38

    Then inside that file, we'll import the

  21. 0:41

    Firebase SDK and initialize it with only

  22. 0:44

    the messaging sender ID. Then we set a

  23. 0:46

    variable with the Firebase messaging

  24. 0:48

    library. That's actually all we need to

  25. 0:50

    do to set up the background worker.

  26. 0:52

    Everything else will happen

  27. 0:53

    automatically. However, to ensure

  28. 0:55

    compatibility with modern browsers, we

  29. 0:57

    need to add a manifest.json file. Inside

  30. 1:00

    this file, we include the GCM sender ID,

  31. 1:04

    which is the same for all applications.

  32. 1:05

    So, keep it the same as it appears in

  33. 1:07

    this video. And then you'll want to link

  34. 1:09

    the manifest in the head of your

  35. 1:11

    index.html file. To ensure these two

  36. 1:14

    newly created files get included when we

  37. 1:16

    build the app, we want to add them to

  38. 1:17

    the Angular CLI.json file.

  39. 1:22

    With the initial setup done, we can

  40. 1:24

    start building the service. We're going

  41. 1:26

    to need both the Angular Fire database

  42. 1:28

    and Angular Fire O modules as well as

  43. 1:30

    the Firebase SDK. We're going to keep

  44. 1:33

    track of the most recent message with a

  45. 1:35

    behavior subject, which is just an

  46. 1:37

    observable that we can push new data to.

  47. 1:39

    So the way cloud messaging works is the

  48. 1:41

    browser will first ask the user for

  49. 1:43

    permission for an app to send them cloud

  50. 1:46

    messages and then Firebase will give us

  51. 1:47

    a token once that permission has been

  52. 1:49

    granted. What we need to do is save that

  53. 1:52

    token with the user's O ID on the

  54. 1:54

    Firebase database so we can use it on

  55. 1:56

    the back end when we start sending

  56. 1:57

    messages with Firebase Cloud Functions.