Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Push - How to Automatically Open an activity without user action on receiving a push on Android

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.

like image 835
Abdul Vajid Avatar asked Dec 19 '22 17:12

Abdul Vajid


1 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));
  }
}
like image 88
makovkastar Avatar answered Jan 25 '23 14:01

makovkastar