Fireship · @fireship

Add Toast Message Notifications to your Angular App

Uploaded July 4, 2017 · Length 4:27

Add Toast Message Notifications to your Angular App

Summary

Add toast message notifications to an Angular app using Firebase and Bulma CSS.

  • angular toast notifications
  • firebase database
  • bulma css
  • angular services
  • toast messages

Transcript preview

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

  1. 0:00

    in this episode we're going to build

  2. 0:02

    toast style notification messages from

  3. 0:04

    scratch the messages will be saved on

  4. 0:06

    the back end in Firebase as well so we

  5. 0:09

    can maintain a full history of the

  6. 0:10

    messages that have been sent the

  7. 0:12

    messages are managed by a central

  8. 0:14

    service that can be injected into any

  9. 0:16

    component this makes it possible to

  10. 0:18

    trigger messages from anywhere in the

  11. 0:20

    app on the front end we're using the

  12. 0:23

    Bulma CSS framework but you could easily

  13. 0:25

    Swap this out for bootstrap or material

  14. 0:28

    or your own custom classes

  15. 0:31

    let's start by building the toast

  16. 0:32

    service we'll first start by importing

  17. 0:36

    the angular fire database then we'll

  18. 0:38

    create a custom message class just to

  19. 0:40

    model our data a message can have a

  20. 0:43

    Content as a string a style and it can

  21. 0:46

    also be dismissed which is a Boolean

  22. 0:48

    value in the Constructor we send it the

  23. 0:50

    content and an optional style argument

  24. 0:53

    if the style is undefined it'll just

  25. 0:55

    default to

  26. 0:57

    info in the service we create a function

  27. 0:59

    to return the Firebase list observable

  28. 1:02

    with the messages in the database we

  29. 1:04

    want to limit this query to the last

  30. 1:06

    five

  31. 1:07

    messages from there we create a function

  32. 1:10

    to create a new message this function

  33. 1:12

    can be called anywhere from any

  34. 1:14

    component and it'll update the Firebase

  35. 1:16

    list

  36. 1:17

    observable it simply takes the content

  37. 1:20

    and style as arguments and builds a new

  38. 1:22

    message and sends that to the

  39. 1:24

    database and the last step is to give

  40. 1:27

    the user the ability to dismiss messages

  41. 1:29

    so we just retrieve a message as an

  42. 1:31

    object and then update its dismissed

  43. 1:34

    attribute to

  44. 1:37

    True before we start building the

  45. 1:39

    component I'm going to build a custom

  46. 1:41

    pipe that will filter out the dismissed

  47. 1:43

    messages and then send them in reversed

  48. 1:45

    order so they display one on top of the

  49. 1:48

    other first I import the filter and

  50. 1:51

    reverse functions from low Dash and the

  51. 1:54

    value going into this pipe will be an

  52. 1:55

    array of messages so we can call reverse

  53. 1:59

    and then we can fill filter out the