This is how my setup looks.
The LunchActivity has code:
Parse.initialize(this, "MY_APP_ID", "MY_APP_KEY");
PushService.subscribe(this, "MyCity", HomeActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
HomeActivity class is a simple activity class that opens a simple screen used as default. I have also written a custom receiver.
public class CityPushReceiver extends BroadcastReceiver {
private static final String TAG = "CityPushReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
JSONObject json = new JSONObject(intent.getExtras().getString(
"com.parse.Data"));
Integer event_id = Integer.parseInt((String) json.get("event_id"));
Intent eventIntent = new Intent(context, EventResult.class);
eventIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
eventIntent.putExtra("event_id", event_id);
context.getApplicationContext().startActivity(eventIntent);
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
Manifest file has entry:
<receiver
android:name="com.myapp.CityPushReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.myapp.CITY_NOTIFICATION" />
</intent-filter>
</receiver>
I use Python code to push the notification:
import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps({
"channels": [
"MyCity"
],
"data": {
"action": "com.myapp.CITY_NOTIFICATION",
"alert": "New Event Notification",
"event_id": "425"
}
}), {
"X-Parse-Application-Id": "APP_ID",
"X-Parse-REST-API-Key": "API_KEY",
"Content-Type": "application/json"
})
result = json.loads(connection.getresponse().read())
print result
This setup is not working as expected. I do get the notification on my device (I am using AVD for testing). But it opens the expected EventResult
activity even without me clicking on the notification in the tray. This happens even if I am on device home screen and the app is running only in background. And when I click on the notification in the tray it opens the HomeActivity
class which is defined as default class.
Expected behaviour is opening the EventResult
only when I click on the notification in the tray. Can you guys tell me what needs to change?