Reputation: 1714
I'm new to Parse, and have made an app that receives push notifications. I have a json payload that I am sending to the app which contains a URL. When the push notification is received by the device, an alert dialog appears with "cancel" and "OK" buttons. If the user presses "OK", then the webpage is loaded.
This works perfectly fine when the application is open on the device, but fails when the application is not open on the device. The notification still appears and the user can click on it to open the application when it is in the background, but no dialog will appear on the screen. If the app is fully closed, then I see a popup telling me that the application has crashed, but then the application will load but no dialog box will be visible on the screen. Here's what my receiver looks like:
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
public static final String ACTION = "custom_action_name";
@Override
public void onReceive(Context context, Intent intent) {
try {
...
...
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
...
if (key.equals("targetUrl")){
MainActivity.showdialog(json.getString(key));
}
...
}
String message = json.getString("alert");
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
so when the iterator hits the targetUrl
key, the showdialog() method is called and passes in the url.
Here is my showdialog method, found in my MainActivity class:
public static void showdialog(final String url){
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
context.startActivity(intent);
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
}
Again - this works fine when the app is open, but fails if the app is closed. How can I have the user open up the app through the notification and see this dialog?
Here is the error stack if the application is closed and I try to open through the push notification:
FATAL EXCEPTION: main
Process: ././., PID: 30566
java.lang.RuntimeException: Unable to start receiver ././.MyCustomReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2856)
at android.app.ActivityThread.access$1700(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
at ././.MainActivity.showdialog(MainActivity.java:140)
at ././.MyCustomReceiver.onReceive(MyCustomReceiver.java:34)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2845)
Upvotes: 1
Views: 2254
Reputation: 278
I had the same issue with Parse Notifications. Here's how I got mine to work.
from there your alertdialog function should work.
public void onReceive(final Context context, final Intent intent) {
currentActivity = ((Launch) context.getApplicationContext()).getCurrentActivity();
if (currentActivity == null) {
Intent notificationIntent = new Intent(context, Login.class);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);
final Handler h = new Handler();
final int delay = 1000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
onReceive(context, intent);
}
}, delay);
}
Edit: After further testing this real hacky solution- I found out that if you have multiple notifications pending, this method will create alertdialogs for every single pending notification and put them on top of each other... if anyone can figure out how to only make the chosen notification show up, I'd be forever grateful! more info here- https://stackoverflow.com/questions/26020523/displaying-a-parse-com-push-notification-with-alert-and-title-keys-as-an-ale
Upvotes: 3