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?
I found that taking control over the notification creation was best on Android. If you don't send a title or alert field in the push then the parse.com android SDK won't create a notification and you can create one yourself when the push comes in.
This was the method I was using to create a notification:
void CreateNotication(Context context, String title, String pushId) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MyClass.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(pushId, 1, mBuilder.build());
}
Use like this :
public class Application extends android.app.Application {
public Application() {
}
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
PushService.setDefaultPushCallback(this, MainActivity.class);
}
}
//MainActivity.java - Activity you need to open when the notification is clicked.
In your Manifest file add this application.
<application
android:label="@string/app_name"
android:name="com.parse.tutorials.pushnotifications.Application"
android:theme="@style/AppTheme">
<activity
android:label="@string/app_name"
android:name="com.parse.tutorials.pushnotifications.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And add this line in your Activity class which you need to open when the notification is clicked.
ParseAnalytics.trackAppOpened(getIntent());
Check this Answer : LINK
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With