I have a requirement (android) where my app should run its main activity automatically when a push notification is received without the user clicking on notification in system tray. I am having a map where it shows current location, but in the push, i will receive a location, and i need my map in the main activity to move the camera to the currently received location on receiving the push, and alert the user with a custom sound. All this should happen without the user clicking on anything. Pls help. Thanks in advance.
-
2That would be extremely user hostile thing to do, requirements or not. – Seva Alekseyev Mar 05 '14 at 17:13
-
you can find all in [the documentation](https://parse.com/docs/push_guide#receiving/Android) ... have you read it? – Selvin Mar 05 '14 at 17:14
-
@SevaAlekseyev - This is not a general user facing app, its for internal purpose of employees..so the user should be friendly to this. :) – Abdul Vajid Mar 06 '14 at 01:54
-
@Selvin i had read the documentation..but using that when a notification comes in, it does nothing but create a notification in system tray and the user needs to click on that..but i want some action to happen automatically when push is received...pls help – Abdul Vajid Mar 06 '14 at 01:56
2 Answers
Yes, it's possible. Parse.com documentation says:
You can also specify an Intent to be fired in the background when the push notification is received. This will allow your app to perform custom handling for the notification, and can be used whether or not you have chosen to display a system tray message. To implement custom notification handling, set the Action entry in your push notification data dictionary to the Intent action which you want to fire. Android guidelines suggest that you prefix the action with your package name to avoid namespace collisions with other running apps.
So you send push notifications in this way:
JSONObject data = new JSONObject("{\"action\": \"com.example.UPDATE_STATUS\""}));
ParsePush push = new ParsePush();
push.setData(data);
push.sendPushInBackground();
Then in your AndroidManifest.xml register a broadcast receiver that will be called whenever a push notification is received with an action parameter of com.example.UPDATE_STATUS:
<receiver android:name="com.example.MyBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.UPDATE_STATUS" />
</intent-filter>
</receiver>
In your broadcast receiver you can start a new activity:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, MyActivity.class));
}
}

- 19,192
- 5
- 36
- 77

- 5,000
- 2
- 30
- 50
-
yes, i did the same already and works well...thanks for coming in, but could you please help me a bit more, as i am looking for enabling/disabling a button in my activity using this broadcast receiver when i recieve a push. If the app is not shown, i should bring it to foreground and enable the button, else just enable the button...i dont want two instance of same activity to be runing. Could u pls help ? – Abdul Vajid Mar 12 '14 at 14:44
-
UPDATE: Got the answer, i used LocalBroadCastManager to send a broadcast to my Activity, and in the onrecieve of the local broadcast receiver, enabled/disabled the button. thanks everyone. – Abdul Vajid Mar 13 '14 at 03:35
-
if you have more than "action", do you know how to retrieve all the other member of the JSON object ? – Tsunaze Oct 21 '14 at 09:02
warning to @makovkastar, major changes since version V1.8. for instance: no more com.example.UPDATE_STATUS.
push notifications guide is more clear now: https://www.parse.com/tutorials/android-push-notifications
this is the ParsePushBroadcastReceiver subclass: https://gist.github.com/panchicore/97d5ad25842258576109 this answer have a good tutorial to send/receive local broadcasts: https://stackoverflow.com/a/8875292/155293
Basically onPushReceive will be called when a push is received in the device, in this method use LocalBroadcastManager to make something in the app, for instance, add a new message to a chatroom.

- 1
- 1

- 11,451
- 12
- 74
- 100