Search code examples
androidpush-notificationparse-platformgoogle-cloud-messaging

Parse.com disable push notification notification


I want to handle the push notification without showing it in the notification bar, what Parse.com does by default.

Parse.com requires the entry in manifest, so I can't change it to my own BroadcastReceiver, because It will not work.

    <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND"
            android:priority="1">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="packagename" />
        </intent-filter>
    </receiver>

So what I have done is creating the second receiver for the same IntentFilter, but with higher priority.

    <receiver android:name="packagename.ParseComPushReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:priority="2">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="packagename" />
        </intent-filter>
    </receiver>

And there I try to abort the broadcast

@Override
public void onReceive(final Context context, final Intent intent) {
    abortBroadcast();
    // handle here
}

But unfortunately, it I still see the "Notification received" notification in the notification bar.


Solution

  • You can actually use your own custom Broadcast Receiver with Parse. It's documented in the Push Guide here.

    Specifically, replace:

        <receiver android:name="com.parse.ParsePushBroadcastReceiver"
            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>
    

    With:

      <receiver
            android:name="com.example.MyCustomReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.example.UPDATE_STATUS" />
                <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>
    

    And implement your own receiver:

    public class MyCustomReceiver extends ParsePushBroadcastReceiver {
    private static final String TAG = "MyCustomReceiver";
    
      @Override
      public void onReceive(Context context, Intent intent) {
        try {
          String action = intent.getAction();
          String channel = intent.getExtras().getString("com.parse.Channel");
          JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
    
          // Custom behavior goes here
    
        } catch (JSONException e) {
          Log.d(TAG, "JSONException: " + e.getMessage());
        }
      }
    }