phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.92k forks source link

additionalData.foreground is false when app is blocked (not in background) by an OS alert in iOS #1233

Open malwatte opened 7 years ago

malwatte commented 7 years ago

I came across an issue while I was testing in iOS 10.

  1. If the app is in background (by pressing home button) the notifications are shown in notification bar. When I clicked the notification, app is opened and "notification" event is fired. additionalData.foreground value is false.

    • App is blocked by an OS alert. This happened to me when OS showed "Apple ID Verification" alert. App is still visible behind the alert but greyed out. The app is not really in background because I can see animations and and other UI activities happens (such as loading data and populating lists) even if app is blocked by the OS alert.
    • push is received while app is blocked. "notification" event is not fired yet.
    • "Not Now" of the OS alert is pressed to dismiss it.
    • "notification" event is fired as soon as OS alert is dismissed. additionalData.foreground value is false.
    • I believe additionalData.foreground value need to be true in this case

Problem is I want to differentiate these two scenarios separately. How do I identify if the app is opened by clicking a notification in notification bar OR blocked by an OS alert? Because in both the cases additionalData.foreground is false.

Device- iPad mini (OS v 10.0.1) Plugin version- 1.8.2 xcode version- 7.1

payload:

{ "data": {
        "Id": "1",
        "notId": 1
        },
     "notification" : {
          "body" : "Ticket created",
          "title" : "Tickets"
        },
     "priority":"high",
      "registration_ids" :["xxxxx"]
    }

Thank you.

(sorry for bad english)

macdonst commented 7 years ago

@malwatte if there is an iOS system dialog on top of your app then the OS reports your app as being in the background. It's a quirk of iOS and there isn't anything I can do about it. You can also reproduce it by pulling down the notification shade.

malwatte commented 7 years ago

@macdonst In my application,

Do you know a way to distinguish app is opened by a notification tap vs system dialog dismiss? Or is there a way we can put the notification on the shade if the app is blocked by a system dialog?

As per your comment if there is an iOS system dialog on top of your app then the OS reports your app as being in the background. But if the app is in background why the push is not shown in system notification shade? Is it the expected behaviour?

Thank you. Greatly appreciate your help.


I found that we would be able to identify if app is blocked by an iOS dialog (or notification header) using both UIApplicationStateBackground and UIApplicationStateActive.

if (application.applicationState != UIApplicationStateActive && application.applicationState != UIApplicationStateBackground) {
        NSLog(@"Foreground Inactive");
    }

Would it be possible to pass additional parameter to "notification" event handler to indicate above state (Foreground Inactive)?

macdonst commented 7 years ago

@malwatte that is really good info. Thanks for digging into that. I'll have to put some thought in how to indicate that in the notification event. Right now we are using foreground which is a boolean so it doesn't really give you the option of a third state.

macdonst commented 7 years ago

See #851

malwatte commented 7 years ago

@macdonst I am glad if above info would help to improve the plugin. I am not good at Objective C. But I can try to go through the code and try to come up with a solution too. Thank you.

malwatte commented 7 years ago

@macdonst Any chance you were able to look into this? :)

malwatte commented 7 years ago

@macdonst , I was wrong about the above if() condition. I have 0 knowledge in Objective C. Sorry. Actually application.applicationState is an enum. https://developer.apple.com/reference/uikit/uiapplicationstate

I believe we can do this in didReceiveRemoteNotification

if(application.applicationState == UIApplicationStateActive){
      // App is in foreground and active
}
else if(application.applicationState == UIApplicationStateInactive){
     // Show the notification in the shade OR save it to use in pushPluginOnApplicationDidBecomeActive
}
else {
     // App is in background
}

Thoughts?

samimaliks commented 7 years ago

I am facing the same issue, when app is foreground notification handler calls automatically. Do you know how to stop it or check if the app is currently running or in background?

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.