我一直在尝试在我的iOS和Android应用上实现parse.com推送通知。我已经成功地在iOS上实现了这一点,但是,我仍然在努力实现Android。当用户收到通知并且用户按下通知时,我想打开我的“HomeScreen”活动,并在警报对话中显示通知中的文本。 Receiver处理程序和清单文件与LogCat一起显示在下面。我做错了什么,我该如何解决?
HomeScreen Java:
public class Receiver extends ParsePushBroadcastReceiver {
public Receiver(){
}
private static final String TAG = "MyNotificationsReceiver";
@Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, HomeScreen.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
String notificationText = json.getString("alert");
AlertDialog Alert= new AlertDialog.Builder(context)
.setTitle("News")
.setMessage(notificationText)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
})
.show();
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
清单:
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</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.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="au.gov.nsw.shellharbour.saferroadsshellharbour" />
</intent-filter>
</receiver>
<receiver android:name="au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$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>
logcat的:
java.lang.RuntimeException: Unable to instantiate receiver au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2260)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
编辑:由于Receiver是一个内部类,因此它不能存在于其外部类的实例之外。这就是您无法通过清单定义实例化它的原因。要解决此问题,请将Receiver标记为static
,或将其定义为自己的类文件。 (您可以删除我之前建议的空构造函数。)
java.lang.InstantiationException:无法实例化类au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen $ Receiver; 没有空构造函数 在android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
您需要一个构造函数,以便可以实例化接收器:
public Receiver(){
}