Unable to receive push notification in Android usi

2019-04-25 07:28发布

问题:

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



}

回答1:

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>


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

Checking your installation object with device-token. (maybe you should set senderID)

below seems deprecated. I can't find any where from parse docs now. try to add following permissions

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
        android:name="com.appname.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
<uses-permission android:name="com.appname.permission.C2D_MESSAGE" />

These are needed permissions for old sdk version (maybe older than 1.10).

==============New part====

I have found the push sample code on github. https://github.com/ParsePlatform/PushTutorial/tree/master/Android

Below is the project which use 1.11.0 sdk(1.13.0 can work,too). https://dl.dropboxusercontent.com/u/82261293/Appname.zip

In my experience, some devices have chance that without device token. You should check it have been saved.



回答6:

Parse will be fully retired.

I got mail from parse that they are going to retired. So why are you doing that mistakes that is going to expired. Try using gsm or socket that is more lighter.