Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to receive push notification in Android using Parse

In Parse console it shows Delivery report as successful, but onPushReceive is not getting called at all. What am I missing here? any help would be appreciated.

Below is my code. Manifest code:

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.appname" />
        </intent-filter>
    </receiver>

    <service android:name="com.parse.PushService" />

    <receiver android:name="com.appname.PushReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </intent-filter>
    </receiver>

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

            <!--IMPORTANT: Change "com.parse.starter" to match your app's package name.-->

            <category android:name="com.appname" />
        </intent-filter>
    </receiver>

PushReceiver class:

public class PushReceiver extends ParsePushBroadcastReceiver
{

@Override
protected void onPushReceive(Context context, Intent intent) {
    super.onPushReceive(context, intent);

    Log.v("Push Receive called...", "");


    if (intent == null)
        return;

    try
    {
        JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));



    } catch (JSONException e) {
        e.printStackTrace();
    }
}



}
like image 284
Anirudh Avatar asked May 12 '16 08:05

Anirudh


4 Answers

Add the permission:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

Check the receiver:

<receiver android:name="com.appname.PushReceiver" android:exported="false">
    <intent-filter>
        <action android:name="com.appname.INTERCEPT"/>
    </intent-filter>
</receiver>
like image 111
t0mm13b Avatar answered Nov 19 '22 02:11

t0mm13b


In Addition To The Permissions

There are a few things that cause Parse to not properly send the push notification. Parse does not do a great job at detailing them, but from what I have experienced with Parse, i can try to shed some light on things that are a not always so obvious.

If you are checking the Parse dashboard and are able to see your device register, that is good. However, you need to make sure that there is a deviceToken registered for that device. If there is not, I can almost guarantee that if you check your pushes tab, you will see that the success rate of that push was 0% or unsuccessful.

Making sure deviceToken is registering Other than simply initializing Parse, please make sure that the package name of your app matches that name which you use to link with in your Android Manifest. If that is not the case, that is most certainly a cause of not receiving pushes.

Intializing Parse

    private void initializeParse() {
         Parse.enableLocalDatastore(this);
         Parse.initialize(this);
         ParseInstallation.getCurrentInstallation().saveInBackground();
         Log.d(LOG_TAG, "Initializing parse with app id: " + getString(R.string.parse_app_id) + " client_key: " + getString(R.string.parse_client_key));
}

Listening For Pushes

public class ParsePluginReceiver extends ParsePushBroadcastReceiver {
private static final String TAG = "ParsePluginReceiver";
private static final String RECEIVED_IN_FOREGROUND = "receivedInForeground";
private static int badgeCount = 0;
public static final String FORWARD_EXTRA = "FORWARD_ACTIVITY";


@Override
protected void onPushReceive(Context context, Intent intent) {
    JSONObject pushData = getPushData(intent);

    if (pushData != null) {
        Log.d(TAG, "JSON Payload: " + pushData.toString());
        super.onPushReceive(context, intent);
    }
}

protected void onPushOpen(Context context, Intent intent) {
    JSONObject pushData = getPushData(intent);
    Log.d(TAG, "JSON Payload: " + pushData.toString());

    if (pushData != null) {
        try {
            super.onPushOpen(context, intent);
        } catch (Exception ex) {

        }
    }
 }

As reference, Parse Sdk is located here: https://parse.com/docs/android/guide

Side Note Parse is to be deprecated start of next year.

like image 23
paul_hundal Avatar answered Nov 19 '22 02:11

paul_hundal


make sure you have all these permissions:

   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:protectionLevel="signature"
        android:name="your_package_name.permission.C2D_MESSAGE" />
    <uses-permission android:name="your_package_name.permission.C2D_MESSAGE" />

And you need to add all of these to your Manifest:

<service android:name="com.parse.PushService" />
    <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>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

Just some advise, move on to something such as Firebase. Parse is shutting down.

like image 2
Ali Bdeir Avatar answered Nov 19 '22 04:11

Ali Bdeir


Here're permissions you should have in your manifest:

<!-- Push Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:protectionLevel="signature"
        android:name="your_package_name.permission.C2D_MESSAGE" />
    <uses-permission android:name="your_package_name.permission.C2D_MESSAGE" />

Here're other stuff you should define in your manifest:

<service android:name="com.parse.PushService" />
        <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>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

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

Gradle dependencies:

compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'

With this manifest configuration i can successfully receive pushes from parse.

like image 2
savepopulation Avatar answered Nov 19 '22 03:11

savepopulation