Last active
September 16, 2016 03:31
-
-
Save ifaour/0b6a35f8eb6a8e2867f9 to your computer and use it in GitHub Desktop.
Example custom parse push receiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?