Skip to content

Commit fca5290

Browse files
committed
Update receiver
1 parent c2add91 commit fca5290

File tree

1 file changed

+72
-65
lines changed

1 file changed

+72
-65
lines changed

app/src/main/java/com/test/MyCustomReceiver.java

+72-65
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,77 @@
1414
import java.util.Iterator;
1515

1616
public class MyCustomReceiver extends BroadcastReceiver {
17-
private static final String TAG = "MyCustomReceiver";
18-
public static final String intentAction = "com.parse.push.intent.RECEIVE";
17+
private static final String TAG = "MyCustomReceiver";
18+
public static final String intentAction = "com.parse.push.intent.RECEIVE";
1919

20-
@Override
21-
public void onReceive(Context context, Intent intent) {
22-
if (intent == null) {
23-
Log.d(TAG, "Receiver intent null");
24-
} else {
25-
// Parse push message and handle accordingly
26-
processPush(context, intent);
27-
}
28-
}
29-
30-
private void processPush(Context context, Intent intent) {
31-
String action = intent.getAction();
32-
Log.d(TAG, "got action " + action );
33-
if (action.equals(intentAction))
34-
{
35-
String channel = intent.getExtras().getString("com.parse.Channel");
36-
try {
37-
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
38-
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
39-
// Iterate the parse keys if needed
40-
Iterator<String> itr = json.keys();
41-
while (itr.hasNext()) {
42-
String key = (String) itr.next();
43-
// Extract custom push data
44-
if (key.equals("customdata")) {
45-
// Handle push notification by invoking activity directly
46-
launchSomeActivity(context, json.getString(key));
47-
// OR trigger a broadcast to activity
48-
triggerBroadcastToActivity(context);
49-
// OR create a local notification
50-
createNotification(context);
51-
}
52-
Log.d(TAG, "..." + key + " => " + json.getString(key));
53-
}
54-
} catch (JSONException ex) {
55-
Log.d(TAG, "JSON failed!");
56-
}
57-
}
58-
}
59-
60-
public static final int NOTIFICATION_ID = 45;
61-
// Create a local dashboard notification to tell user about the event
62-
private void createNotification(Context context) {
63-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(
64-
R.drawable.ic_launcher).setContentTitle("Successfully logged in");
65-
NotificationManager mNotificationManager = (NotificationManager) context
66-
.getSystemService(Context.NOTIFICATION_SERVICE);
67-
mNotificationManager.notify(45, mBuilder.build());
68-
}
69-
70-
// Handle push notification by invoking activity directly
71-
private void launchSomeActivity(Context context, String datavalue) {
72-
Intent pupInt = new Intent(context, ShowPopUp.class);
73-
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
74-
pupInt.putExtra("customdata", datavalue);
75-
context.getApplicationContext().startActivity(pupInt);
76-
}
77-
78-
// Handle push notification by sending a local broadcast
79-
// to which the activity subscribes to
80-
private void triggerBroadcastToActivity(Context context) {
81-
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(intentAction));
82-
}
20+
@Override
21+
public void onReceive(Context context, Intent intent) {
22+
if (intent == null) {
23+
Log.d(TAG, "Receiver intent null");
24+
} else {
25+
// Parse push message and handle accordingly
26+
processPush(context, intent);
27+
}
28+
}
29+
30+
private void processPush(Context context, Intent intent) {
31+
String action = intent.getAction();
32+
Log.d(TAG, "got action " + action);
33+
if (action.equals(intentAction)) {
34+
String channel = intent.getExtras().getString("com.parse.Channel");
35+
try {
36+
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
37+
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
38+
// Iterate the parse keys if needed
39+
Iterator<String> itr = json.keys();
40+
while (itr.hasNext()) {
41+
String key = (String) itr.next();
42+
String value = json.getString(key);
43+
Log.d(TAG, "..." + key + " => " + value);
44+
// Extract custom push data
45+
if (key.equals("customdata")) {
46+
// create a local notification
47+
createNotification(context, value);
48+
} else if (key.equals("launch")) {
49+
// Handle push notification by invoking activity directly
50+
launchSomeActivity(context, value);
51+
} else if (key.equals("broadcast")) {
52+
// OR trigger a broadcast to activity
53+
triggerBroadcastToActivity(context, value);
54+
}
55+
}
56+
} catch (JSONException ex) {
57+
Log.d(TAG, "JSON failed!");
58+
}
59+
}
60+
}
61+
62+
public static final int NOTIFICATION_ID = 45;
63+
// Create a local dashboard notification to tell user about the event
64+
// See: http://guides.codepath.com/android/Notifications
65+
private void createNotification(Context context, String datavalue) {
66+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(
67+
R.drawable.ic_launcher).setContentTitle("Notification: " + datavalue).setContentText("Pushed!");
68+
NotificationManager mNotificationManager = (NotificationManager) context
69+
.getSystemService(Context.NOTIFICATION_SERVICE);
70+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
71+
}
72+
73+
// Handle push notification by invoking activity directly
74+
// See: http://guides.codepath.com/android/Using-Intents-to-Create-Flows
75+
private void launchSomeActivity(Context context, String datavalue) {
76+
Intent pupInt = new Intent(context, ShowPopUp.class);
77+
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78+
pupInt.putExtra("data", datavalue);
79+
context.getApplicationContext().startActivity(pupInt);
80+
}
81+
82+
// Handle push notification by sending a local broadcast
83+
// to which the activity subscribes to
84+
// See: http://guides.codepath.com/android/Starting-Background-Services#communicating-with-a-broadcastreceiver
85+
private void triggerBroadcastToActivity(Context context, String datavalue) {
86+
Intent intent = new Intent(intentAction);
87+
intent.putExtra("data", datavalue);
88+
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
89+
}
8390
}

0 commit comments

Comments
 (0)