Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnResume from background, all my notification disappear but no Event Call Back #525

Open
clarklight opened this issue Apr 2, 2015 · 36 comments

Comments

@clarklight
Copy link

Hi

I am wondering how do i handle onResume functions.
When i resume the app by either clicking on the Icon, the push notifications from the tray, all get wiped, but there are no event call back, none of the message get appeneded, they just disappeared.

For the foreground

if (e.foreground) {

            $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

            // if the notification contains a soundname, play it.
            var my_media = new Media("/android_asset/www/"+e.soundname);
            my_media.play();
            }

For Resume

would i do
function onResume() {
onNotificationGCM = function(e) {
$("#app-status-ul").append('

  • --INLINE NOTIFICATION--' + '
  • ');
    }
    }
    document.addEventListener("resume", onResume, true);

    ??

    @samvrlewis
    Copy link

    Your onNotification function should be called when your app isn't active and you click the notification icon. From what I understand, e.foreground will be true if the app is open when the notification is received. Otherwise, e.coldstart will be true if the app hasn't been started. If the app has been started but has been minimised, e.coldstart and e.foreground will both be false. The onNotification function in the example code demonstrates this well.

    @clarklight
    Copy link
    Author

    @samvrlewis But what happen when i click the App Icon to resume the App? I am still confused because in some of the comment like #423, the last comment by Gionni, he said in the latest version 2.4.0, the issue has been fixed? So i should be able to just click the App icon to resume to the app and by using onResume, i should be able to trigger a ECB? Because at the moment, when i resume the App by clicking the App icon, it clears my notification Tray, but there is no ECB.

    @clarklight
    Copy link
    Author

    I have window.onNotificationGCM = function(e) {} then i call if (e.foreground) {} else{ if (e.coldstart){}else{}

    Not sure how am i supposed to call for ECB or if i am able to do so when i resume by clicking the App icon but not the notification. (Documentation didn't mention it for version 2.4.0)
    At the moment when i resume the App(by clicking the App Icon not the push tray), it doesnt do anything and when i put the App back to the background,(the "pause" action) it clears the tray automatically. So i never receives the notification(if i click on the App icon), and i have to click on the notification to resume, but i want to be able to allow the users to click the App icon to resume and still have an ECB.

    @samvrlewis
    Copy link

    Sorry, misunderstood what you were asking. If you're developing for Android can you modify the plugin as Gionni does here: #423 (comment) ?

    @clarklight
    Copy link
    Author

    @samvrlewis
    No prob, there are 2 points.
    1.) I want to see/confirm if a fix on the Java is needed or not(as i am not sure on the standard behavior for version 2.4.0) Gionni first mentioned the fix(comment link you attached) will fix it, but in the end of that thread,he mentioned the fix is not needed, as the ECB for onResume should be a standard behavior. "marek1 say 2.4.0 solved all. If it's standard behavior, what is to fix?"(I am very confused, and no indication on Read.me)

    2.) I also need this for IOS(Def) and Win Mobile(possibly).

    I am just wondering if you have any experience on it? And when you resume the App by clicking the App icon, and then putting the App back to the background, does that deletes your tray as well? I am ok with it, as long as it doesnt delete my tray if i put it in the background. (but i still need it to be cleared, if i click on the notification).

    @samvrlewis
    Copy link

    It's hard to be sure but maybe 2.4.0 fixed it on iOS? I'm only using
    android so can't tell. I don't think it is working for android at the
    moment. Would be good if anyone could confirm the behavior in iOS.

    On Tue, 7 Apr 2015 15:59 clarklight [email protected] wrote:

    @samvrlewis https://github.com/samvrlewis
    No prob, there are 2 points.
    1.) I want to see/confirm if a fix on the Java is needed or not(as i am
    not sure on the standard behavior for version 2.4.0) Gionni first mentioned
    the fix(comment link you attached) will fix it, but in the end of that
    thread,he mentioned the fix is not needed, as the ECB for onResume should
    be a standard behavior. marek1 say 2.4.0 solved all. If it's standard
    behavior, what is to fix?(I am very confused, and no indication on Read.me)

    2.) I also need this for IOS and Win Moible(possibly).


    Reply to this email directly or view it on GitHub
    #525 (comment)
    .

    @clarklight
    Copy link
    Author

    @samvrlewis Ahh ok, no problem. But just wondering if that deletes your notification tray as well ?(when you put the App back to the background(if you resume the App by App Icon) but not clicking the notification)...... and 1 problem is i am using Phonegap Build at the moment, so the changes in Java and such i have to move all my other plugins and package them all again in CLI.

    @samvrlewis
    Copy link

    For me if I have a notification then open the app through the app icon the notification remains. However, if I minimise the app again the notification disappears. Definitely not ideal behaviour!

    I am using Cordova instead of PhoneGap though.

    @clarklight
    Copy link
    Author

    @samvrlewis yes, when you minimise it(the "Pause") action triggers the "clear" function. By the way, i was doing some research and found that a few people forked this version and made some changes, and made it into phonegap builds plugin. https://build.phonegap.com/plugins. (search for push, and remove window phone 8)
    I havent tested out any of it, but maybe its worth a look at(if you are interested).

    @samvrlewis
    Copy link

    Ah yep, just found that too. Removing notificationManager.cancelAll() makes it work as expected (why would you want the notifications to disappear if the app is paused??). Thanks for the tip about the forked versions; it's a shame nobody is maintaining this project.

    @clarklight
    Copy link
    Author

    @samvrlewis i just found that line in the pushplugin.Java as well but I am still trying to figure out what i gotta do, because i am using phonegap build.(not packaging it through CLI) And i am trying to find a way, if there are any of those phonegap build version has this notificationManager.cancelAll() line in. So i dont have to transfer every plugin i have into/thru CLI(as i will have to move all my files etc). 1 odd thing is no one is maintaining this project and continuing on the updates, as push notification is pretty important nowadays, and Phonegap has no official version. So without this, Phonegap has "No" push plugins really, which is weird(350k apps are using this plugin, but no one is maintaining it).

    I just realized, even if i remove that line, it might just a temporary fix for me, cos i have to do the same for IOS then Win.....and i have 0 experience in Objective c or swift.

    @samvrlewis
    Copy link

    That line is line 134 of PushPlugin.java, I commented out lines 134 and 135.

    I agree that it is very weird that it's not maintained, would have thought it would've been a priority as well. The Android code even uses a depreciated API from Google. It might be worth asking on the PhoneGap support forums why this plugin isn't being maintained?

    BTW, this pull request might be useful for you as well but I haven't tested it: #279

    @clarklight
    Copy link
    Author

    @samvrlewis Thanks! by the way http://stackoverflow.com/questions/29204334/how-to-handle-push-notification-when-application-is-resumed
    PushPlugin.sendExtras(extras); this line, move that out from the ifinForeground, then the message will pass thru without clicking, so even on Resume the message will go into the ECB if you need it. I will try to dig deeper if i can find anything for IOS and Win Mobile(Maybe if they do have something for it).

    I have actually opened a Thread on Phonegap Forum, about this onResume issue. No reply.
    http://community.phonegap.com/nitobi/topics/push-plugin-onresume-click-app-icon-to-resume-it-deletes-all-my-notification-but-no-ecb
    I guess if you comment on it as well, and if enough people comments on it, they will escalate it to their team.

    Stupidest thing is, (the quickest way for me to achieve it, is by removing those 2 lines, submit the plugin and then i can use phonegap build to package it). But i gotta upgrade my account, i dont even know how to upgrade my account.

    Just wondering, if you tested taking that final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll(); out?? If that works smoothly and all on the phone?

    @samvrlewis
    Copy link

    Yep I think that's similar to Gionni's solution that I linked to before?

    I think alerting PhoneGap that the plugin isn't being maintained at all might be more useful than telling them this specific issue. I'm not sure but it may be the intended behaviour that the notification isn't received until the notification icon is pressed -- posting about that issue might not get much interest.

    I did see that someone else posted on the support forums about this plugin not being maintained/pull requests not being accepted: http://community.phonegap.com/nitobi/topics/official-pushplugin-plugin-have-many-issues-and-pull-requests-must-to-fix-it-and-merge-it. It may be worth raising an issue in the PhoneGap build repo like this one phonegap/build#298 as suggested.

    I did test taking out those two lines. When the lines are removed it just stops the notification disappearing when the app is paused, it doesn't change the onResume behaviour. To get the onNotification event to fire when the app is resumed I think you'll have to use either that StackOverflow solution or Gionni's solution.

    @clarklight
    Copy link
    Author

    Ahh ok, i was actually digging into the Java, i dont really have much experience in Java, just some experience in AS3. I was thinking if for the public void onResume set the PushPlugin.sendExtras(extras); in there, maybe it triggers the ECB passthru. But i am really bad in OOP so.

    i will keep this fix for now. And also raise the "alert" on Phonegap Forum.

    The problem with the stackoverflow fix, is that it turns it into a silent passthrough, i assume. The best behavior would be the one they have on Gmail/Gchat. When on resume the ECB is triggered. But for now. I will have to deal with it for Android, then figure something out for IOS.

    Do you have any experience in submitting plugins in phonegap?

    @samvrlewis
    Copy link

    I just tested the StackOverflow solution; calling PushPlugin.sendExtras(extras) always in onMessage (in GCMIntentService.java) works. If the app isn't in the foreground/active then the extras will be cached and then your ECB is called once the app is open. It's not a silent pass through.

    I don't have any experience in submitting plugins, sorry. Building the app myself with Cordova wasn't very hard though, I assume it's similar for PhoneGap.

    @clarklight
    Copy link
    Author

    Ahh ok, thanks! I was thinking it has something with silent pass-thru.
    It shouldnt be too hard to build it with Phonegap using CLI, i just dont really have much time at the moment to switch things over. So trying to find the quickest way to get everything working.
    So it means that by calling PushPlugin.sendExtras(extras) in onMessage, that fixed everything(just want to 100% clarify). So the foreground and background(by clicking the notification tray) and ALSO onResume by App Icon, all 3 scenarios works? Its a good fix?
    Because if so, i can(first test out the IOS side), if a fix is needed, i can find a IOS guy to patch it up, then i will submit a plugin, so if anyone needs it they can use it, so they dont have to suffer like we did(i did anyways...been researching and such for days) haha

    @samvrlewis
    Copy link

    Yep, so the ecb fires for me if I:

    • Click the notification icon to open the app
    • Open the app through the app icon after a notification is received and the app is paused
    • Open the app through the app icon after a notification is received and the app is not running at all

    The only slight problem is that if you have a notification, open the app through the app icon and then click the notification icon, the ecb will fire both times. If this isn't desired behaviour you could fix by cancelling the notifications when the cached extras are sent.

    Also, you'll still need the other fix I mentioned in #525 (comment) if you don't want notifications cancelled when the app is paused.

    I'd be very interested to hear what the behaviour is on iOS, let me know how you go with it. I have a feeling it may be totally different to Android and just work as we expected. I think this project was merged in from multiple different plugins so each platform probably has different behaviour.

    @samvrlewis
    Copy link

    Also, if you want a notification icon to appear while the app is in the foreground (this doesn't happen by default), you should add createNotification(context, extras); in after line 68 of GCMIntentService.java.

    Also not sure if you've noticed but if you send the phone a second notification then it overwrites the first one. This is as notId is always set to 0 in createNotification. You can change by setting it to something different each time (maybe current UNIX time in ms or something?).

    @clarklight
    Copy link
    Author

    ok, cool. So 2/3 points

    -) Double ECB firing - when you open the App thru App icon then clicking the notification icon
    (BUT CAN BE FIXED?? by Adding final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();
    In the public void onResume part in Pushplugin.Java??
    That should remove the notification tray on resume but also keeping the ECB. As you add in the stackoverflow fix. (I hope it doesnt remove the notification before the ECB get passed thru)

    -) Then by removing the above part(notificationManager.cancelAll(); ) from onPause.

    Then that should mean, when you onResume(whatever way) the ECB fires, then deletes the Push Notification tray. And on Pause it does nothing(So it doesnt cause any conflict, thinking there are something to delete).

    The notID part, yes thats a problem for Android. I heard its not a problem for IOS. But i had been just generating an ID n+1 each time and when it hits 10,000, it starts back from 0. So it kind of patches it up.

    I will have to let you know about IOS in 2 or 3 days, swamped with work at the moment, and this haha.

    I also commented on Phonegap forum to ask for some info on upgrading my account, i think its just 9.99 and i will patch this all up,fork this and submit a plugin.

    @samvrlewis
    Copy link

    Yep, I guess cancelling them in onResume would work as well. I think cancelling them in the if block starting on line 74 of PushPlugin.java might be a bit better as you're calling that function a bit less (rather than every time you resume)? But it probably doesn't matter too much.

    The only issue is that I guess it's a bit confusing for the user. If I'm using an app I don't expect my notifications to disappear when I open the app. I guess it depends on what your app is doing though. A better way might be to not put the extras with the notification intent. That way the notification remains but just acts as a way to open your app. I'll have to have a think about the best way to implement it for what I'm working on.

    @clarklight
    Copy link
    Author

    Haha yer sorry, i had a plan in my mind, i was only wanting to pay the 9.99 is that i can submit 2 versions for this into Phonegap if they allow me.
    1.)it is that for the notification to pass thru when on resume, and the notification disappears( this is the version i need, i need to force feed data into the App, so i can use phonegap build for it).
    I am assuming the notification disappears, but the ECB is fired(by clicking App Icon), then i can just create a popup, so the users knows the data went into their App.

    2.)And the second version i will keep the notification in the tray and delete the clear tray when onPause.
    So the notification won't be passed through only until it is clicked(to avoid the double fire issue).
    Then the users can have a choice to what they need!

    I will keep you updated and on the IOS version as well.

    But let me know if the cancelling("cancelAll"remove notification tray onResume) causes any conflict(because of the bubbling event,if it happen), if the ECB is not completed the pass through then the delete notification is triggered). Well i think it shouldnt, but if it does, let me know thanks!

    @clarklight
    Copy link
    Author

    I am still playing with the pushplugin, and i am not sure if it is me, or if it is a general conflict. But my pushplugin doesn't work with Single Instance Launch Mode, only works with SingleTop.

    @clarklight
    Copy link
    Author

    Hi, it's been a while, i am just wondering if anyone has any experience/knowledge on silent pass through for Android(for phonegap push plugin)?
    I see that there are work done for IOS #288
    But seems like they have not mentioned anything about Android.

    @samvrlewis
    Copy link

    Fairly straight forward on Android as you need to explicitly create a notification. Look at GCMIntentService.java. If you just want your Javascript callback function to fire without a notification popping up on the device, just remove the call to createNotification. If you want to create the notification only sometimes you'll have to include some sort of flag in your notification and then check that in the onMessage Java function.

    @clarklight
    Copy link
    Author

    Cool, thanks, but i am not 100% sure on it though, i am thinking to have the Event call back to trigger even when the phone is in the background,(The phone receives the notification, the user does not touch the phone, but ecb still fires and trigger an Ajax in the App). Also creating a notification and put into the notification tray. Do you mean that? I am a little confused sorry.

    The PushPlugin.sendExtras(extras); in GCMIntentService.java is the part where it triggers the data from the push to be added into the App right? So i guess if i do this?

               else {
                extras.putBoolean("foreground", false);
    
                // Send a notification if there is a message
                if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                    createNotification(context, extras);
             PushPlugin.sendExtras(extras);
                }
    

    @matt-way
    Copy link

    matt-way commented Jul 8, 2015

    I am having similar problems with the plugin too. I noticed that the android clear on pause issue might have been a simple mistake on the devs part. I believe the two clear lines (134-135) should be in the resume event instead (which would make for more standard functionality).

    @maic76
    Copy link

    maic76 commented Aug 5, 2015

    i did the PushPlugin.sendExtras(extras) in GCMIntentService.java and it worked!! Thanks guys this saved my life!... i just im a little confuse about what the Extras mean??? in the documentation of the plugin there is no explanation, i cant find it.... what exactly im sayin to the PushPlugin to do when i put the sendExtras??

    @kishorpawar
    Copy link

    I fixed this issue here #314
    I have explained in detailed

    @lakshaydulani
    Copy link

    This issue is fixed in the new plugin
    For Android, you need to set clearNotifications:false, which is default true.

    @arshad104
    Copy link

    Hi everyone,

    I am new to ionic and using pushPlugin for push notifications. I want to call a function when I receive notification. Currently I am receiving notification and calling function when user tap the notification. How can I achieve this to call a function when I receive notification (means without tapping notification) irrespective of app in foreground/background? any help will be appreciated. Thanks

    @pawarkishor
    Copy link

    @arshad104 go here

    @arshad104
    Copy link

    @pawarkishor Thank you for quick response. Can I just clone and replace this plugin?

    @arshad104
    Copy link

    @kishorpawar Where can I put the following code?

    if(ALREADY_REGISTERED) { pushNotification.setECB(success, error, { "senderID" :"SENDER_ID_HERE","ecb": "window.onNotificationGCM" }); } else { pushNotification.register(success, error, { "senderID" :"SENDER_ID_HERE","ecb": "window.onNotificationGCM" }); }

    My code is:

    `pushNotification = window.plugins.pushNotification;

    window.onNotification = function(e) {
    
      //alert('notification received');
       
      switch(e.event){
        case 'registered':
          if(e.regid.length > 0) {
            device_token = e.regid;
            //alert(device_token);
            NotificationService.register(device_token).then(function(response){
               console.log('registered!');
            });
          }
        break;
    
        case 'message':
          //alert('msg received');
          NotificationService.acknowledge(e.payload.msg_key).then(function(response){
            console.log('acknowledged!');
            //alert(JSON.stringify(response));
          });
        break;
    
        case 'error':
          console.log('error occured');
        break;
    
      }
    };
    
    window.errorHandler = function(error){
      console.log('an error occured');
    };
    

    pushNotification.register(onNotification, errorHandler, config);`

    I am confused at this point. Please help?

    @kishorpawar
    Copy link

    kishorpawar commented Dec 13, 2016 via email

    @arshad104
    Copy link

    @kishorpawar Thank you for your help. I am unable to get notification, just getting vibration when notification received, no notification on status bar? what could be the fix? any idea?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    8 participants