Here is an example!
public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); Log.d(TAG, json.getString("alert").toString()); final String notificationTitle = json.getString("title").toString(); final String notificationContent = json.getString("alert").toString(); final String uri = json.getString("uri"); Intent resultIntent = null; TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); resultIntent = new Intent(context, HomeScreen.class); stackBuilder.addParentStack(HomeScreen.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Following this guide, replace the android name for the recipient with the path of your custom translator
<receiver android:name=".receivers.ParseCustomBroadcastReceiver" android:exported="false" > <intent-filter> <action android:name="com.parse.push.intent.RECEIVE" /> <action android:name="com.parse.push.intent.DELETE" /> <action android:name="com.parse.push.intent.OPEN" /> </intent-filter> </receiver>
In your application class, be sure to register for Parse Push, as shown below.
Parse.initialize(this, ParseUtils.PARSE_APPLICATION_ID_DEV, ParseUtils.PARSE_CLIENT_KEY_DEV); ParseObject.registerSubclass(Article.class); ParsePush.subscribeInBackground("", new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { Log.d(TAG, "successfully subscribed to the broadcast channel."); } else { Log.e(TAG, "failed to subscribe for push", e); } } });
Update:
The aforementioned broadcast receiver expects to send a notification about a json object from the parsing pane, usually consisting of three pairs of name values:
{ "alert": "Alert message", "title": "Title", "uri": "bla bla" }
When you use only a regular string, for example: hello world! in the syntax panel, click the panel, internally it will become only a warning tag. something like this: {"alert" : "hello world!, "title" = null/"", "uri" = null/""}
The broadcast receiver code above does not have these null checks, so it gets caught with the exception No Title / uri.
So, you can add some null checks based on what is expected from the push notification.
Something like that:
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); Log.d(TAG, json.getString("alert").toString()); if (json.has("title")) { notificationTitle = json.getString("title").toString(); } if (json.has("alert")) { notificationAlert = json.getString("alert").toString(); } if(json.has("uri")) { notificationURI = json.getString("uri"); }
Then set the appropriate contentTitle and contentText in the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(notificationAlert) .setContentText(notificationAlert);
Hope this helps!
