0

I'm developing an phonegap-based android app, and I'm writing code to handle notifications. Here is a snippet of the piece of code which is supposed to raise the notification:

Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
        PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

I want to prepare the Intent so it opens the app in the page related to what the notification is about. So far, the only thing I have found related to this is the following line, placed in the MainActivity class (the one which extends DroidGap class), in the onCreate method:

super.loadUrl("file:///android_asset/www/index.html", 10000);

but I would like to set that URL dynamically from the code above, or, in the worst (or best, I really dont know...) case, parametrize it and pass parameters to the onCreate method from the code above. How do I do that? Thanks in advance...

Throoze
  • 3,988
  • 8
  • 45
  • 67

1 Answers1

4

You can pass the url as an extra on your intent:

 Intent notificationIntent = new Intent(context, MainActivity.class);
 notificationIntent.putExtra("url", "file:///android_asset/www/page_you_want_to_load.html")

 // set intent so it does not start a new activity
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_SINGLE_TOP);

Then on your activity overload the onNewIntent mehotd to receive it, and call super.loadUrl. If you register your activity as SINGLE_TOP like you did, the method onNewInent(intent) will be called if the activity was already created.

public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();

if(extras != null){
    if (extras.containsKey("url"))
      //load the url in the parameter
      super.loadUrl(extras.getString(url));
   }
else
   //fall back in case nothing is given, load the default url.
   super.loadUrl("file:///android_asset/www/index.html");    
}

EDIT

You should call onNewIntent from your onCreate activitiy:

  public void onCreate(){
     super.onCreate(savedInstanceState);
      onNewIntent(getIntent());          
}

You have three possible cases:

1- Your activity is not running. The user clicks on the app Icon, starts your app. onCreate() runs as expected, getIntent() will return the intent that was used to create your app. There is no url extra defined in that intent, so you'll fall back to the default page.

2- Your activity is not running. It ran in the past, the notification was raised but for any reason the activity was killed. The user has the notification in the bar. So he clicks on the notification and starts your activity. onCreate() runs as expected, but this time you DO have a url' extra in your intent, soonNewIntent` will open the url you defined as extra on your intent.

3- Your activity is running. It was created, now it still exists but it's on the background paused. The user clicks on your notification and brings it up. onCreate is not called because your activity already exists. but onNewIntent is. With the intent you defined. So there is a url extra, and that url is loaded on onNewIntent.

Refer to this question for more details!

Community
  • 1
  • 1
caiocpricci2
  • 7,714
  • 10
  • 56
  • 88
  • Thanks for your answer! It is not clear to me what I have to do in my `onCreate()` method. Can I access the current `Intent` (and check for extras) in the `onCreate()` method? How do I do that? Thanks again! – Throoze May 24 '13 at 10:33
  • Thanks for your complete answer! One mmore question, just to be sure: If the app is in background, and is awaked by the user without any notification existing, `onNewIntent()` is called, isn't it? then it would fall back to the default page? I know this could be obvious, but Im new with android developement, and I need to be sure... – Throoze May 24 '13 at 11:53
  • I'm not sure about the answer to that but my first guess is yes. It will display whatever was being displayed before. A very easy way of testing it is pressing the home key and then opening the app again. Post your findings here please! – caiocpricci2 May 24 '13 at 11:58
  • `super.loadUrl(extras.getString(url));` should be `super.loadUrl(extras.getString("url"));` – Pataar Nov 04 '14 at 14:03