How to override onPushReceive () ParsePushBroadcastReceiver?

I am using the Parse.com service push alert. According to doc :

override onPushReceive to trigger background operation for silent pushes

I found the onPushOpen () source code here , but now I need to redefine onPushReceive () to adjust the sound and vibration behavior, I don’t know what I have to do in onPushReceive (), is there any code example that will help me figure it out in the logic inside onPushReceive ()? Thank.

+4
source share
2 answers

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;
   }
}

:

  <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>

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

+16

"" , .

onPushReceive:

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);
    }

}

, "" "".

, - push...

+2

All Articles