English subtitles

← 05-08 Receiving a Message!

05-08 Receiving a Message!

Get Embed Code
4 Languages

Showing Revision 2 created 10/01/2015 by Udacity Robot.

  1. Now we are ready to receive
    Downstream Messages.
  2. We're going to use a broadcast receiver
    because our job is fairly simple.
  3. However, if your app needs to do
    a fair amount of work after receiving
  4. a message, you might want to use
    a wakeful broadcast receiver.
  5. A wakeful broadcast receiver
    is a special type of broadcast
  6. receiver that manages a partial
    wake log for your app.
  7. It passes off the work of processing
    the GCM message to a service
  8. typically an intense service while
    ensuring that the device does not go
  9. back to sleep in the meantime.
  10. If you don't hold a wake lock while
    transitioning the work to a service
  11. the device could go back to
    sleep before the work completes.
  12. Recall that the wake lock
    permission was optional?
  13. This is why.
  14. You need it if you're going to
    use a wakeful broadcast receiver.
  15. But since we're going to use a broadcast
    receiver we won't be needing
  16. that permission.
  17. In our broadcast receivers
    onReceive method,
  18. we're going to need to process our
    message and then use that to build
  19. an alert notification that
    we display to the user.
  20. Now, there are many different types
    of messages that can come through
  21. Google Cloud Messaging.
  22. So, just look for the one that you want
    in your app and ignore the others.
  23. In this case we want a MESSAGE
    that comes from our sender.
  24. This means that we will know how
    to properly parse our message, and
  25. that it will contain all of
    the bits that we're expecting.
  26. So we're going to then use those
    bits to build up a string,
  27. and we're going to pass that
    along to our helper function.
  28. To create our notification.
  29. You recall how to build notifications,
    right?
  30. The main differences you want to
    focus on is here I have the content
  31. intent directing back to the main
    activity for Sunshine, so
  32. it just reopens the weather app and
    you can look for more information.
  33. The other thing to note is the priority.
  34. Here I've set the priority to high,
    and what that means is that on
  35. a range of negative two to two,
    I've set the priority to one.
  36. Because this is an alert, and
    it's potentially important for users.
  37. And that's why I did this.
  38. You want to be really
    careful with priorities, so
  39. you don't end up spamming your users and
    so
  40. that they don't miss
    things that are important.
  41. But then, everything's ready to go.
  42. We fire that off, and
    our receiver is done.