5

I am currently investigating in an online-offline-supported application and figuring out the usage of APNS, and it is quite easy to understand when my application only deals with single user.(just like SMS)

My understanding is, each application installed to the specific device will have its own device token (and which won't be changed even after unregistration to APNS and re-register again). So, when a user login from different devices, APNS and in turn the server can identify.

However, when I tried to implement a multiple account apps ( just like Facebook, that different people can login using same phone), some questions arose.

When user A logged in and when the app is in background state, yea it did receive the notification.

But when user A logged out (when the phone has no internet connection, which means can't update the server at once for deletion of account), and then user B logged in, meanwhile the server tried to push notification to the phone for user A, the notification has already received by the phone (but that's notification for user A) and user B would receive that (which should never happen in real scenario).

It seems that APNS cannot check in-application account authentication.

So, my questions are,

  1. Is there any way to check and remove the notification on the phone (I've read the Apple Doc. and notice that there is a QoS storing the last notification)?

  2. what is a good practice to handle such kind of authentication problems upon using APNS?

Please, if any, correct me if I do have any conceptual misunderstanding. And hope that my questions don't sound stupid and clumsy and there's not any question duplication ...

2 Answers2

3

If you have a feature to 'log out' from your app (without logging in a new user), I can't find any way to stop push notifications being sent except by letting the server know that the user has logged out.

If there is no internet connection, no proper logout can be made. If you need to have this feature, you should verify the logout with the server. The user could be informed that the logout failed since the server could not be reached.

Here is an example:

  • User A logs in on device X that has device token TX.

    -> Server associates token TX to belong to user A.

  • User A logs in on device Y that has device token TY.

    -> Server associates tokens TX and TY to belong to user A.

  • Push message sent to user A

    -> Server sends push message to TX and TY.

  • User A logs out from device X and user B logs in on device X.

    -> Server removes association of token TX to user A

    -> Server associates token TX to user B.

  • Push message sent to user A

    -> Server sends push message to TY.

  • Push message sent to user B

    -> Server sends push message to TX.

  • User B logs out, but has no internet connection

    -> Login failed, user B still logged in, receives dialog "Could not log out"

jake_hetfield
  • 3,388
  • 1
  • 23
  • 30
  • Thanks! However, just as I've mentioned in the comment under another answer, my app requires offline features (and actually my app is an offline-dominated one..... so somehow can't prompt the user logout failed just because they didn't connect to server.... looks strange...) Wonder how Facebook allows it Orz – user1516507 Aug 03 '12 at 05:21
2

So, at first:

How user A can logout and login in Facebook without internet?

When user B is login, therefore internet is connected, then you can send to your server: now userB at this device token.

And for case: user B logged in, meanwhile the server tried to push notification to the phone for user A

If app is runned, then APNS notification you will get at

application: (UIApplication*)app didRegisterForRemoteNotificationsWithDeviceToken: (NSData*)deviceToken

not in device notificaion center message window.

And you can to do this:

1) If userA already in log, then send Local Notification for show a notification

2) If a other user in log OR users is logouted, then to do nothing

Hope it's helped. Maybe i am wrong, if you mean a other.

CReaTuS
  • 2,593
  • 1
  • 19
  • 31
  • Thanks : ), maybe the example of Facebook is not that good xD Because my app will support features that can be accomplished in offline mode. I did tried to log out with no internet connection from Facebook App, and somehow it DID log out for me .__. – user1516507 Aug 03 '12 at 01:07
  • And, bad idea to send Push notif in depend for online users. You can use Local Notifications, for good results. But, i see, you need to use a APNS – CReaTuS Aug 03 '12 at 02:06
  • Oh! Do you mean that, instead of making use of the default "alert" of APNS, I should change my strategy to something in this way: Server still asked APNS to push notification to the device, without showing any kind of alert view as well as increment of badge number, but instead sending some user info like "receiver":"john@gmail.com" and the check if the just logged in user is john@gmail.com or not. If Yes, make good use of local notification, else do nothing. Is that what you mean? – user1516507 Aug 03 '12 at 11:15
  • I mean,why you using Apns? You can check a user log in and log out with using any POST , GET postes. and if true user, send Local notification who will come after 24 hrs. If user will log out, you can at easly cancel Local notif and send a new. – CReaTuS Aug 03 '12 at 16:03
  • Oh I use it because I want to prompt the user immediately about, for example, there's a new item is available for retrieval when the user is not using my app (app in background). – user1516507 Aug 06 '12 at 01:18
  • easly way. One account from one device. If man want to play, then will buy a new device for someself =) – CReaTuS Aug 06 '12 at 01:49
  • Haha this is for sure safest way to avoid the "bugs", and so really adore how the big developers can handle such problem with ease... Anyways, thanks for your suggestions they are helpful! =] – user1516507 Aug 06 '12 at 01:53