I am using push notification service of Parse.com. According to the doc:
override onPushReceive to trigger a background operation for "silent" pushes
I found the source code of onPushOpen() here, but now I have to override onPushReceive() to customize the behavior of sound and vibration. I don't know what I should do in onPushReceive(), is there any sample code that help me figure out the logic inside onPushReceive()? Thanks.
Create a new class that extends ParsePushBroadcastReceiver:
public class MyPushBroadcastReceiver extends ParsePushBroadcastReceiver {
public static final String PARSE_DATA_KEY = "com.parse.Data";
@Override
protected Notification getNotification(Context context, Intent intent) {
// deactivate standard notification
return null;
}
@Override
protected void onPushOpen(Context context, Intent intent) {
// Implement
}
@Override
protected void onPushReceive(Context context, Intent intent) {
JSONObject data = getDataFromIntent(intent);
// Do something with the data. To create a notification do:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.ic_notification);
builder.setAutoCancel(true);
// OPTIONAL create soundUri and set sound:
builder.setSound(soundUri);
notificationManager.notify("MyTag", 0, builder.build());
}
private JSONObject getDataFromIntent(Intent intent) {
JSONObject data = null;
try {
data = new JSONObject(intent.getExtras().getString(PARSE_DATA_KEY));
} catch (JSONException e) {
// Json was not readable...
}
return data;
}
}
Add this in your Manifest:
<receiver
android:name=".MyPushBroadcastReceiver"
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>
Further information: http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/
You can use the intent extra parameter "action" to call your intent to handle whatever you want.
Original onPushReceive source:
protected void onPushReceive(Context context, Intent intent) {
JSONObject pushData = null;
try {
pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
} catch (JSONException var7) {
Parse.logE("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", var7);
}
String action = null;
if(pushData != null) {
action = pushData.optString("action", (String)null);
}
if(action != null) {
Bundle notification = intent.getExtras();
Intent broadcastIntent = new Intent();
broadcastIntent.putExtras(notification);
broadcastIntent.setAction(action);
broadcastIntent.setPackage(context.getPackageName());
context.sendBroadcast(broadcastIntent);
}
Notification notification1 = this.getNotification(context, intent);
if(notification1 != null) {
ParseNotificationManager.getInstance().showNotification(context, notification1);
}
}
And no notification if no "alert" or "title" extra in the intent.
So you do not need to extend any class at all for silent push updates...
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