0

I am working on a senior project in pubnub android api. The example application works with toasting when a message is received. However,I want to start another activity when a message is received. Basically, It is a home alarm system where the message comes from raspberry pi. Upon receiving the message, a new activity should display where the user can enter a pin code that was used to lock the system to be able to stop the alarm. Everything works as desired only when the application is running. I can't get the new activity to show when the application is not running, even if it receives a message. How can I achieve that? I am starting the alarm activity in a runOnUiThread() then set startAlarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP) as suggested in this post: Start Activity from a Broadcast Receiver. This does not work for me.

Community
  • 1
  • 1
user3422517
  • 93
  • 2
  • 9

1 Answers1

1

I would suggest you to use Google cloud messaging (as a push notification service).

When the event happens in the Raspberry you can send a message to the android device using GCM. The messaging approach is better than creating a periodic task in order to check if there are new messages.

In the example they create a notification in the notification bar, but you can start a new activity instead of creating a notification. In this link you can find how to start a new activity: http://developer.android.com/training/basics/firstapp/starting-activity.html

  • On the raspberry side the easiest way for sending the message to the device might be using just curl:

    curl -X POST \ -H "Authorization: key= YOUR_AUTHORIZATION_KEY" \ -H "Content-Type: application/json" \ -d '{ "registration_ids": [ "YOUR_DEVICE_TOKEN" ], "data": { "message": "YOUR_MESSAGE" } }' \ https://android.googleapis.com/gcm/send

Anjz
  • 197
  • 1
  • 1
  • 11