Skip to content Skip to sidebar Skip to footer

How To Override Onpushreceive() Of Parsepushbroadcastreceiver?

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

Solution 1:

Create a new class that extends ParsePushBroadcastReceiver:

publicclassMyPushBroadcastReceiverextendsParsePushBroadcastReceiver {

publicstatic final StringPARSE_DATA_KEY = "com.parse.Data";

   @OverrideprotectedNotificationgetNotification(Context context, Intent intent) {
      // deactivate standard notificationreturnnull;
   }

   @OverrideprotectedvoidonPushOpen(Context context, Intent intent) {
      // Implement       
   }  

   @OverrideprotectedvoidonPushReceive(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 = newNotificationCompat.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());

   }

   privateJSONObjectgetDataFromIntent(Intent intent) {
      JSONObject data = null;
      try {
         data = newJSONObject(intent.getExtras().getString(PARSE_DATA_KEY));
      } catch (JSONException e) {
         // Json was not readable...
      }
      return data;
   }
}

Add this in your Manifest:

<receiverandroid:name=".MyPushBroadcastReceiver"android:exported="false"><intent-filter><actionandroid:name="com.parse.push.intent.RECEIVE" /><actionandroid:name="com.parse.push.intent.DELETE" /><actionandroid:name="com.parse.push.intent.OPEN" /></intent-filter></receiver>

Further information: http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/

Solution 2:

You can use the intent extra parameter "action" to call your intent to handle whatever you want.

Original onPushReceive source:

protectedvoidonPushReceive(Context context, Intent intent) {
    JSONObject pushData = null;

    try {
        pushData = newJSONObject(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 = newIntent();
        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...

Post a Comment for "How To Override Onpushreceive() Of Parsepushbroadcastreceiver?"