2

Take an app which is an e-mail client and gets push notifications when a new e-mail has arrived.

How do these apps make sure that when a push notification has arrived it's for the currently logged-in user?

For example, user logs-in with user1 then logs out and logs in with user2. What if a push notification related to a new e-mail for user1 arrives when user2 is logged in?

From the push notification communication mechanism point of view, this is possible. The push notification can be already on the way when user switches logins.

The problem is on iOS when a new push notification has arrived, your code doesn't get called if the app is not in foreground.

This changed a bit with iOS 7 (https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW10) yoru code gets called, but the push notification is still displayed without your code, meaning you can't decide weather to display it or not. This is different than Android where you can always decide.

WriteEatSleepRepeat
  • 3,083
  • 3
  • 33
  • 56
  • You register a device token during push notification and I might be wrong (I haven't done this yet) but I think you need the server to send the correct notification based on the device token. So, I would say create a table/collection where it maps User IDs to device tokens and from there do what you need to do per user. – Gasim Apr 24 '15 at 21:20
  • 1
    I know how push notifications and device registrations work. The issue I'm trying to describe is that a push notification can arrive at a time when your app might be a state which is different than one related to the received push notification. And if the app is in background state, iOS automatically displays the push notification, without having your code to decide that. – WriteEatSleepRepeat Apr 24 '15 at 21:23

1 Answers1

2

This is true, you cannot catch the notification before the system, but... you can instead send a silent push notification, where its payload is something like this:

{
    "aps": {
        "content-available": 1
    },
    "user": "someUser",
    "alertMsg": "someMessage"
}

which essentially will trigger your application:didReceiveRemoteNotification:fetchCompletionHandler: and give you 30 seconds for processing (if you're on the background) without presenting an alert. You can now check if the logged-in user matches and if it does, fire a local notification with the information from the remote one (or even with data from a network call)

I hope that this makes sense...

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • Curiosity question: Is that how Facebook Messenger does it with their notifications? When I view a message from my laptop, the notification on the phone automatically gets removed. – Gasim Apr 24 '15 at 21:37
  • 1
    @Gasim Although only a fb engineer could provide a definite answer to that, I would bet that this is how they do it (it is the official and recommended way to get notified for remote content availability after all) – Alladinian Apr 24 '15 at 21:40
  • @AndreiN. Did they? I cannot recall a third party app that filtered remote notifications before 7 :/ I would also be curious to know the answer if this is true though – Alladinian Apr 24 '15 at 21:41
  • @Alladinian that's what I'm asking, on earlier than iOS7, how did all the apps make sure user won't see a wrong notification when app is not in foreground. – WriteEatSleepRepeat Apr 24 '15 at 21:43
  • 1
    I guess they didn't ? :) – WriteEatSleepRepeat Apr 24 '15 at 21:45
  • @AndreiN. They probably didn't (iOS6 didn't even have APIs for background execution) – Alladinian Apr 24 '15 at 21:45
  • so technically, another user could had received a push notification related to another user. I know chances are small, but technically it's possible. – WriteEatSleepRepeat Apr 24 '15 at 21:47
  • @AndreiN. I know that it happened to me in the past :P Yes that happened (and probably still happens in some apps) – Alladinian Apr 24 '15 at 21:49
  • To be honest, I haven't even seen Notifications getting removed before iOS8... And @AndreiN., I don't think that push notifications are actually designed to work for that situation. I actually have never seen anyone who would even lend their phone to another person yet let them log the owner out of an app and login with their own credentials... – Gasim Apr 24 '15 at 21:49
  • 2
    @Gasim Think multi-account apps like Twitter (also this is a more common scenario for an iPad app, where the device is used by several family members). I agree though that they shouldn't be abused and that Apple would have to provide a proper mechanism for that scenarios – Alladinian Apr 24 '15 at 21:51
  • 1
    Ok now I see your point :) I was looking from security point of view at first that the only problem this situation causes is annoyance (like I hate how Telegram app doesn't remove the notifications when I already read them on my laptop). And for the iPad scenario, let's hope that Apple adds multiple user accounts (aside from Push API extensions) in iOS9 – Gasim Apr 24 '15 at 21:58
  • @Alladinian it looks like silent push notification doesn't work when app is app is force-quitted. http://stackoverflow.com/questions/19068762/will-ios-launch-my-app-into-the-background-if-it-was-force-quit-by-the-user https://devforums.apple.com/thread/209664?tstart=0 did you know about this? this is a bummer because makes my scenario impossible to implement – WriteEatSleepRepeat Apr 29 '15 at 19:16
  • @AndreiN. Yes (this is in the docs as well), though it doesnt make your scenario impossible. It is just another 'hurdle' (along with permissions for notifications & background fetching) between the information and the user (from your point of view) but a result of an explicit user action (from her point of view) and ultimately her/his decision... – Alladinian Apr 30 '15 at 06:58
  • @Alladinian since silent notifications are not always delivered (when app is killed), I can't use them. I can't rely on user being aware that killing the app will stop notifications from being displayed. Moreover, if I can't use silent notifications then this means I can't check and decide whether or not to display the notification, I am back to same problem. Am I missing something? – WriteEatSleepRepeat Apr 30 '15 at 09:38
  • @Alladinian I'm sure a very large majority of people have no idea that killing an app stops it to receive notifications. I just don't understand how apps are supposed to handle my scenario correctly. In iOS 8 there is PushKit which seems to be the solution. The problem is there is still a big chunk of iOS 7 users. – WriteEatSleepRepeat Apr 30 '15 at 09:55
  • @AndreiN. Correct, but this is Apple's decision for its platform (remember, it's not a bug, Location based apps _do_ launch in the background even after a force quit...). The difference from the original approach is that with silent notifications you can be sure that a message will be displayed only to currently-logged-in users, on the other hand, yes, you will lose any messages to users that did a force quit. – Alladinian Apr 30 '15 at 10:07
  • Thanks. So there's no 100% reliable solution to my scenario. Again, I wonder how all the big companies are implementing this scenario, I wonder if they do. – WriteEatSleepRepeat Apr 30 '15 at 10:30