打开Parse推送通知时出现异常

时间:2014-10-02 05:19:58

标签: android push-notification parse-platform

我设置Parse push Android SDK 1.7.0并且可以毫无问题地接受推送。但是,当打开推送通知时,我的应用程序崩溃时出现以下异常:

  

android.content.ActivityNotFoundException:找不到要处理的Activity   Intent {act = android.intent.action.VIEW dat = flg = 0x1000c000(has   额外的)}

我还尝试使用getActivity()继承ParsePushBroadcastReceiver并始终返回主活动类,结果相同。下面是完整的堆栈跟踪:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ers.test, PID: 13513
java.lang.RuntimeException: Unable to start receiver com.ers.test.MyPushBroadcastReceiver: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2452)
        at android.app.ActivityThread.access$1700(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
        at android.app.Instrumentation.execStartActivitiesAsUser(Instrumentation.java:1481)
        at android.app.ContextImpl.startActivitiesAsUser(ContextImpl.java:1136)
        at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:344)
        at android.content.ContextWrapper.startActivitiesAsUser(ContextWrapper.java:344)
        at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:221)
        at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:232)
        at android.app.TaskStackBuilder.startActivities(TaskStackBuilder.java:208)
        at com.parse.TaskStackBuilderHelper.startActivities(TaskStackBuilderHelper.java:19)
        at com.parse.ParsePushBroadcastReceiver.onPushOpen(ParsePushBroadcastReceiver.java:202)
        at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:108)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2445)
        at android.app.ActivityThread.access$1700(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:102)

花了几个小时。找到了解决方案: 实现你的接收器并扩展ParsePushBroadcastReceiver类。

Receiver.java

public class Receiver extends ParsePushBroadcastReceiver {

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Log.e("Push", "Clicked");
        Intent i = new Intent(context, HomeActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

在清单中使用它,(而不是使用ParsePushBroadcastReceiver)

项目清单代码:

<receiver
            android:name="your.package.name.Receiver"
            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>

快乐编码!!

答案 1 :(得分:32)

此异常的原因是ParsePushBroadcastReceiver想要在此代码中的推送消息中打开空uri:

String uriString = null;
    try
    {
      JSONObject pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
      uriString = pushData.optString("uri");
    }
    catch (JSONException e)
    {
      Parse.logE("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", e);
    }
    if (uriString != null) {
          activityIntent = new Intent("android.intent.action.VIEW", Uri.parse(uriString));
        } else {
          activityIntent = new Intent(context, cls);
        }

如果您只是推送消息,uriString将为空而不为空,因此context.startActivity(activityIntent);将打开一个空的uri并发生异常。

要解决此问题,您可以继承ParsePushBroadcastReceiver(感谢@Ahmad Raza)并覆盖onPushopen,如下所示:

public class Receiver extends ParsePushBroadcastReceiver {

    @Override
    protected void onPushOpen(Context context, Intent intent) {

        ParseAnalytics.trackAppOpenedInBackground(intent);

        String uriString = null;
        try {
            JSONObject pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
            uriString = pushData.optString("uri");
        } catch (JSONException e) {
            Log.v("com.parse.ParsePushReceiver", "Unexpected JSONException when receiving push data: ", e);
        }
        Class<? extends Activity> cls = getActivity(context, intent);
        Intent activityIntent;
        if (uriString != null && !uriString.isEmpty()) {
            activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
        } else {
            activityIntent = new Intent(context, MainActivity.class);
        }
        activityIntent.putExtras(intent.getExtras());
        if (Build.VERSION.SDK_INT >= 16) {
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(cls);
            stackBuilder.addNextIntent(activityIntent);
            stackBuilder.startActivities();
        } else {
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(activityIntent);
        }
    }
} 

并像这样更新Manifest:

<receiver
            android:name="your.package.name.Receiver"
            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>

我希望他们在下次更新时解决问题。