0

I followed a tutorial online on how to receive push notifications on Android. Got everything set up on our server and was able to receive a message and log it.

The class that receives the message is called MessageReceiver and extends BroadcastReceiver. In this class's OnReceive() message I managed to Log.v the message payload correctly.

Now this OnReceive() is not able to do any UI stuff, like for example if I wanted to pop up a window telling the user that a push has come in, I couldn't. I can't even create a Notification from here to put in the phone's toolbar.

What should I do? I don't want to change my message receving code too much as it seems to be working ok. Any ideas on how I can get this method to interact with the UI or call other methods from other .java files? I know I could always create static methods and call them from here, but a lot of the methods I want to call would not work as static as they interact with many other pieces of code.

Thanks.

PaulG
  • 6,920
  • 12
  • 54
  • 98

3 Answers3

1

I think this link should answer your question. Basically, use the Context in the onReceive method.

Fire Notification from Broadcast Receiver

Community
  • 1
  • 1
Kaediil
  • 5,465
  • 2
  • 21
  • 20
  • Yes, you're right. Strangely enough I didn't even realize that it was getting back a Context object. Also didn't realize that this question has already been asked and answered. Thanks a million. – PaulG Dec 02 '11 at 19:44
0

You should be able to add a Notification just fine from a BroadcastReceiver. If you want to really interact with the UI though, you should just start an activity.

kabuko
  • 36,028
  • 10
  • 80
  • 93
0

Recievers cant really affect UI.you need to use activity for that.

Its not blank
  • 3,055
  • 22
  • 37