Cordova Plugin event when receive notification and App is in foreground

I'm using urban airship plugin and when i have the application in foreground the event: urbanairship.push is not fired, the only event that is fired is the incoming messages but only when the application is in background. 


Is there any way to catch the events when the application is in foreground? 

Best regards,
Vitor

Didn't find what you were looking for?

New post

Comments

9 comments

  • Vitor,

    The push event should fire, regardless of background or foreground.

    Are you experiencing this on both iOS and Android? Additionally, could you let us know how you've set up your listeners in your app? Code snippets should work fine.

    Comment actions Permalink
    0
  • Hi Michael, 

    My code is something like that:

    scope.pushCallback = {};

             

            scope.pushCallback.onRegistration = (function(event) {

                if (event.error) {

                    console.log("There was an error registering for push notifications");

                } else {

                    console.log(event.channelID);

                }

            });

             

            scope.pushCallback.onPushReceived = (function(event){            

                console.log("Incoming push: " + event.message);

            });

             

            scope.pushCallback.handleDeepLink = (function(event){

                console.log("Deep link: " + event.deepLink)

            });

    document.addEventListener("urbanairship.registration", scope.pushCallback.onRegistration);

    // Register for any Urban Airship push events

    document.addEventListener("urbanairship.push", scope.pushCallback.onPushReceived);

    document.addEventListener("urbanairship.deep_link",  scope.pushCallback.handleDeepLink);

     

    I'm experiencing this on Android, when the app is in foreground any of the events is fired.

     

    Thanks.

    Comment actions Permalink
    0
  • Vitor,

    When are those event listeners being registered? Are they within the onDeviceReady event?

    Comment actions Permalink
    0
  • It's called on the onReady of my page that only runs after onDeviceReady from Cordova. 

    I tried the sample app that is available in the rep of plugin and i'm not receiving the event too.

    Comment actions Permalink
    0
  • Vitor,

    If you could, could you run adb logcat on your Android device and log when a push is received both in the foreground and background, regardless of whether the event triggers or not?

    I'm looking through the code, and I think I have an idea of what's going on. It appears that, on Android, if the notification is not posted, that is, is not displayed to the user, then the push received event is called with a null notification ID, which could result in the event listener not firing. I'm not terribly certain at the moment if this is expected, but if you have some logs that you can send over, I can communicate that internally and see if that is expected or something we can fix.

    Comment actions Permalink
    0
  • Michael,

     

    You can find here the logs that i caught from ADB. I hope you can help with this information.

     

    Another question, why when i have the app in foreground the notification is always created in notification center? Is it not possible only fire the event to JS?

    Thanks.

    Comment actions Permalink
    0
  • Vitor,

    Could you confirm one thing for me?

    One thing we've seen in the past is that, if at any point in time you have made any changes to your Package ID for your app, this could cause those events to not fire. 

    Regardless, I was able to reproduce the issue you're seeing, using the theory above, and also fixed it by removing the Android platform via the CLI (Cordova platform remove android), and then re-adding the android platform (Cordova platform add android).

    After performing that and rebuilding the project, I was able to see the Push event being fired.

    Go ahead and give that a shot and let me know if that works for you!

     

    Comment actions Permalink
    0
  • Hi, 

    sorry on delay, but today morning i found the issue and it was related with Package Name, that it's used on receiver instead of ApplicationId, as my package name it's different of ApplicationId the receiver didn't work. 

     

    As suggestion, you should change the plugin to use ApplicationId variable from gradle on the plugin.xml:

                <receiver android:name="com.urbanairship.cordova.CordovaAirshipReceiver"
                            android:exported="false">
    
                    <intent-filter>
                        <action android:name="com.urbanairship.push.CHANNEL_UPDATED" />
                        <action android:name="com.urbanairship.push.OPENED" />
                        <action android:name="com.urbanairship.push.DISMISSED" />
                        <action android:name="com.urbanairship.push.RECEIVED" />
                        <category android:name="${applicationId}" />
                    </intent-filter>
                </receiver>

    Thanks for your help.

     

    Comment actions Permalink
    0
  • Vitor,

    No problem, glad to hear it works!

    Comment actions Permalink
    0

Please sign in to leave a comment.