Handling push notification on Android when app is in the foreground (cordova plugin)

The common behaviour on Android when a push notification is received while the app is in the foreground is to "swallow" the notification, so it doesn't show on the notification center and doesn't play the notification sound.

Is it possible to do using the Cordova plugin? If not, how would I add this functionality using native code (ideally without overriding the plugin code so I can update it whenever I need to)?

Thanks

Didn't find what you were looking for?

New post

Comments

7 comments

  • Zvi,

     

    When the app is in the foreground, your notification should appear in the notification center, unless you've modified the plugin to perform some other action when a push is received. I've tested with the sample given on the Phonegap/Cordova github page here:

    https://github.com/urbanairship/phonegap-ua-push

     

    Alternatively, if you want to show content to the user while they have the app foregrounded, you may consider sending along an In-app Message with the contents of the push you just sent them:

    http://docs.urbanairship.com/user-guide/message-composer.html#in-app-message

    http://docs.urbanairship.com/topic-guides/in-app-messaging.html

    It may give the user a better experience depending on your use case.

    Comment actions Permalink
    0
  • Hi,

    I don't want to show content to the user if the app is in the foreground, I just don't want the push to appear in the notification center.

    Can you please direct me to the place I need to modify in the plugin code to be able to do this?

    Comment actions Permalink
    0
  • Zvi,

    We typically do not recommend making modifications to the plugin, since you'll need to re-do those changes when the plugin is updated, and any issues you have with it would be unsupported.

    That being said, while I cannot give you code, you would need to tell your app whether or not you're currently in the foreground:

    http://stackoverflow.com/questions/2314969/how-to-determine-if-one-of-my-activities-is-in-the-foreground

    If the app is in the foreground, you can modify the PushListener(line 192) such that when a push is received, it will only make the callback if the app is not foregrounded.

     

    There is no real simple way around this without changing the behavior of the plugin.

    Comment actions Permalink
    0
  • If I'm not mistaken, the callback in the PushListener simply notifies the javascript side about the push. Preventing it (whether only when the app is in the foreground or globally) won't prevent the push from appearing in the notification center. Am I wrong?

    Comment actions Permalink
    0
  • Zvi,

    Correct, but I was not suggesting that you would prevent it from being called, but rather modifying the behavior such that it is aware of whether or not the app is foregrounded or back grounded. Given that info, it can then decide when to perform the callback to notify the javascript side about the push. Since the broadcast receiver is created in the PushListener, you could modify the code.

    Alternatively, you may also make your own NotificationFactory class that has this behavior, which may cause unexpected behaviors within your app.

     

    At this point, it may be best to allow your users to be able to see the notification icon when the push is received, so they are aware that there is new content to view.

     

    Comment actions Permalink
    0
  • I am not able to get the notification icon in my app. I tried overriding it to a specific logo but it just doenst show up. I have placed all the logo files with same name in all the drawable folder's but UA is not able to pull it up why is that?

    Comment actions Permalink
    0
  • Piyush,

    There could be several reasons for this.

    1. If you have included your own icon files in the drawable folders and have renamed the icons, you'll need to ensure you make those changes in your config.xml file.

    2. You have not re-built your application after changing the icon files. Please be sure you are doing this through the Cordova CLI when making changes to the app.

    3. Clean, re-build your app. May be good to clean the cache on the app on your device.

     

    Lastly, it may be worth it to change the name of the icon file and include it in the appropriate locations in your app, remove the Urban Airship icons rather than replacing them, and reference the icons in the appropriate locations in your Cordova project. Then, do a a clean rebuild of the app and see if that makes any difference.

    Comment actions Permalink
    0

Please sign in to leave a comment.