Exception when opening Parse push notification [closed]
After spending few hours. Found a solution:Implement your receiver and extends ParsePushBroadcastReceiver class.
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); }}
Use it in manifest, (Instead of using ParsePushBroadcastReceiver)
Code for project's 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>
Happy Coding!!
The cause of this exception is when ParsePushBroadcastReceiver
wants to open the empty uri in your push message in this code :
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); }
If you just push a message, the uriString
will be empty not null, So context.startActivity(activityIntent);
will open an empty uri and exception occurs.
To solve this issue you can subclass ParsePushBroadcastReceiver
(thanks to @Ahmad Raza) and override onPushopen like this :
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); } }}
and update Manifest like this :
<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>
I hope they solve the problem in their next update.