Skip to content

Instantly share code, notes, and snippets.

@ifaour
Last active September 16, 2016 03:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifaour/0b6a35f8eb6a8e2867f9 to your computer and use it in GitHub Desktop.
Save ifaour/0b6a35f8eb6a8e2867f9 to your computer and use it in GitHub Desktop.
Example custom parse push receiver
package com.parse.starter;
import android.app.Activity;
import android.os.Bundle;
import com.parse.ParseAnalytics;
public class MyCustomActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
}
}
package com.parse.starter;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.parse.ParsePushBroadcastReceiver;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by ibrahim on 2/24/15.
*/
public class MyReceiver extends ParsePushBroadcastReceiver {
private final String TAG = "PUSH_NOTIF";
@Override
public void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "onPushOpen triggered!");
Intent i = new Intent(context, MyCustomActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
@Override
public void onPushReceive(Context context, Intent intent) {
Log.i(TAG, "onPushReceive triggered!");
JSONObject pushData;
String alert = null;
String title = null;
try {
pushData = new JSONObject(intent.getStringExtra(MyReceiver.KEY_PUSH_DATA));
alert = pushData.getString("alert");
title = pushData.getString("title");
} catch (JSONException e) {}
Log.i(TAG,"alert is " + alert);
Log.i(TAG,"title is " + title);
Intent cIntent = new Intent(MyReceiver.ACTION_PUSH_OPEN);
cIntent.putExtras(intent.getExtras());
cIntent.setPackage(context.getPackageName());
// WE SHOULD HANDLE DELETE AS WELL
// BUT IT'S NOT HERE TO SIMPLIFY THINGS!
PendingIntent pContentIntent =
PendingIntent.getBroadcast(context, 0 /*just for testing*/, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(alert)
.setContentText(title)
.setContentIntent(pContentIntent)
.setAutoCancel(true);
NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationManager.notify(1, builder.build());
}
}
@kkrishnan90
Copy link

Bro nice code ! Clean , crisp and clear ! I've got just one doubt.
How do I add an action button to notification and pass it to the correct broadcast receiver ?? Not able to do it so far. Could you help me out on this ?

@shery9
Copy link

shery9 commented Jan 6, 2016

how would be the json is ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment