Parse-Android;无法在自定义ParseBroadcastReceiver中接收“操作”

时间:2016-06-13 16:57:20

标签: android parse-platform push-notification

我发送了一封包含自定义action的推送消息,但在onPushReceive我得到的操作是com.parse.push.intent.RECEIVE而不是com.hellboy.beat.STATUS_DONE

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hellboy.beat"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <!-- permission required to use Alarm Manager -->
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

    <!-- permission required for Notification -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <!-- permission required to Send SMS -->
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <!-- permission required to get Location & Number -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- permission required to Vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- permission required to use Parse -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- permission required to use Parse Push -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.parse.starter.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.hellboy.beat.permission.C2D_MESSAGE" />

    <application
        android:name="com.hellboy.beat.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.hellboy.beat.main.Activity_main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.hellboy.beat.login.Activity_login" />
        <activity android:name="com.hellboy.beat.requests.Activity_requests" />
        <activity android:name="com.hellboy.beat.settings.Activity_settings" />

        <!-- Register the Notification Receiver -->
        <service android:name="com.parse.PushService" />

        <!-- Regular Push Notification -->
        <receiver android:name="com.parse.ParseBroadcastReceiver" />

        <!-- Our Reciver -->
        <receiver
            android:name="com.hellboy.beat.PushRec"
            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.hellboy.beat.STATUS_DONE" />
                <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" />

                <!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
                <category android:name="com.hellboy.beat" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

PushRec.java

public class PushRec extends ParsePushBroadcastReceiver 
{
    private static final String TAG = "BEAT_PUSHREC";

    Context context;

    @Override
    public void onPushReceive(Context context, Intent intent) 
    {
        this.context = context;

        Log.i(TAG, "Receiver");

        if (intent == null)
        {
            Log.i(TAG, "Receiver intent null");
        }else
        {
            Log.i(TAG, "Receiver intent not null");

            String action = intent.getAction();
            Log.i(TAG, "got action " + action );

            if (action.equals("com.hellboy.beat.STATUS_DONE"))
            {
                //DOES NOT COME OVER HERE
            }
        }
    }
    }

发送-推送:

                ParseQuery<ParseInstallation> pushQuery = ParseInstallation.getQuery();
                pushQuery.whereMatchesQuery("user", subscriberQuery);

                JSONObject obj=null;

                obj =new JSONObject();
                try 
                {
                    obj.put("action","com.hellboy.beat.STATUS_DONE");
                    obj.put("statusdone_message"    , message);
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

                //TODO move this to the 'cloud'
                ParsePush pPush = new ParsePush();
                pPush.setQuery(pushQuery);
                pPush.setData(obj);

                pPush.sendInBackground();

注意:我的理解是,自通知到达后我在Manfiest中犯了一个错误,但错误的action被称为

2 个答案:

答案 0 :(得分:1)

您可以覆盖onReceive方法以调度自定义操作。

  @Override
  public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    switch (intentAction) {
      case "com.hellboy.beat.STATUS_DONE":
        onPushReceive(context, intent);
        break;
      case ACTION_PUSH_DELETE:
        onPushDismiss(context, intent);
        break;
      case ACTION_PUSH_OPEN:
        onPushOpen(context, intent);
        break;
    }
  }

https://github.com/ParsePlatform/Parse-SDK-Android/blob/master/Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java

答案 1 :(得分:1)

错误出现在Manifest中; ParseBroadcastReceiver应该是ParsePushBroadcastReceiver

<强>段:

<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.hellboy.beat.PushRec" >
    <intent-filter>
        <action android:name="com.hellboy.beat.STATUS_DONE" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.USER_PRESENT" />
    </intent-filter>
</receiver>

将其从清单

中删除
   <!-- Regular Push Notification -->
   <!-- REMOVE THIS -->
   <receiver android:name="com.parse.ParseBroadcastReceiver" />

同样在接收者中:

public class PushRec extends ParsePushBroadcastReceiver 
{
}

变为

public class PushRec extends BroadcastReceiver 
{
}