How can I send apple push notifications based on userIDs not only on device tokens? - iphone

I use push notifications in my iOS application but the problem is - several users with different IDs in the system can use the same device and I want to send push notification only to the user which is logged in, but now notifications arrive to the device even if other user is logged in. I know that APNS identifies pushes only by device tokens...
But is there some way to send push notifications based on user ID or other information not only device token?

You'll have to manage it yourself.
Whenever a user logs-in to your app, notify your server (send the user-id to the server).
Do the same whenever a user logs-off.
In your server, based on the currently logged-in user (you'll have to manage a database that contains for each device token the currently logged-in user), you can decide
which push notification to send to the device.

I don't think iOS push notification is right for your problem. here is why
consider you have an app with multiple users using the same device. you have a simple scenario where users can assign tasks to other users and they would like to get notifications when a task has been assigned to them
you do 'user to device token' mapping yourself on the server. consider user A and user B. both registered to receive notifications. so on the server both of them will have an entry in the mapping of what their device tokens are
Now, user B is logged out -- you will update on the server by removing the token for user B? or say use a flag to update the status that he is logged out?
user A now assigns a task to user B. on the server, you can see that user B has no mapping or mapping status is not active?
what happens to the notification?
you will end up queuing a notification for user b until he logs in? and push again? but again how do you know when to push again?
It is better you provide some UX in the app to get the notifications on a tap or periodically poll the server for any notifications

Related

Device to device push notifications logic Swift IOS Firebase

My goal is to implement a function where, when a user (lets call him User1) sends a message to another user (User2), a notification is sent to User2´s device, so he can see the message right on the lock screen. I have already implemented a function where this works. My only problem now is that when I send the push Notification from User1´s device to User2´s device, I don't know if User2 is on the account that he should receive the message on.
If User2 has logged into a different account and User1 sends him a message, he sees this message, although User2 is in a different account and shouldn't see this message. Is there any way to know if a user logged into a different account and to then block the notification from showing on User2´s device?
Firebase Cloud Messaging has no concept of a user. It only knows about devices, or more explicitly app instances (a specific app on a specific device is an app instance).
If your use-case is based around users, your application logic is making a mapping from a user to their FCM instance ID/IDs. If you want the user to not receive a message anymore on a specific app instance, you need to remove the mapping you made.
The most common way to do this is to remove the mapping when the user signs out from your application on a specific device.
Since this is all rather abstract, I recommend also checking out:
When to register an FCM token for a user
Removing a user from Firebase
How to send FCM messages to a different user
Android: How to handle user logout Firebase Cloud Messaging in 2021
How to get Firebase user id from FCM token? (in admin code on server)

Send reminder notifications for user's birthday

I am creating an app using Swift and Parse as my backend platform. In my application a User can send an invitation to be connected to another user and as soon as they are connected the app should send notifications to remind the user about his friend’s birthday.
In this scenario I have two moments in which the app should send a notification:
1- When user A sends an invitation to user B, user B should receive a notification
2- When it’s user’s B birthday, user A should receive a notification and vice versa
I created a Parse object called AppNotification that is responsible for storing information about the notifications the user should receive. So when a user sends an invitation to another user I am adding a line in the AppNotification table, the same happens when it’s a birthday of a friend. Using this I can calculate the number of unread notifications and show it to the user in the UI using the components badge.
My question is more related to the best way to send the birthday notification. What I am considering is:
1- When user A sends and invitation to user B I have to send a push notification to user B.
2- As soon as the users are connected I can schedule local notifications to remind them about the birthday. This solution would use Local Notifications instead of Push Notifications.
3- Or create a Job in Parse that will be executed every day and will read all the users whose birthday is today and send a push notification to his friends. This solution would use Push notifications and I would not need to worry about scheduling local notifications.
Between the points 2 and 3 which one is the best solution? Is there any other approach I could use? When should I consider using Local Notifications other than Remote Notifications?
Thank you in advance.
Regarding points 2 and 3: there are pros and cons for both options: Having the reminders as local notifications is basically what your local calendar app does, i.e. you don't need a connection the Internet / Parse. However: what happens if one party (A) decides that she no longer wants to be a "friend" (of B)? Should notifications still be sent? If not, you could easily delete the scheduled notification on the server. This would also ensure that the other party (B) wouldn't receive notifications of a former friend (A) if she didn't open the app in the meantime and synced the list of friends.
There are of course more pros and cons, so it is really a matter of your preference and possibly other constraints (e.g. should an active internet connection be necessity?).

How should I handle a multiple accounts app with APNS?

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,
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)?
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 ...
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"
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.

Could one iOS application support multiple push notification registration?

This is the first time that I approach the push notification service and I'm little bit confused. I 'd like to have just some conceptual help, not code.
I need to build an app that should receive and register for different kind of notifications. For instance in my app I'd like that users could register for PROMO notifications category and NEWS notifications category, I'd like that they could choose which one they want to be notified.
Reading the Apple doc, that was not so clear to me, it seems that once the app device is registered I receive just one token and seems impossible to receive more tokens for different kind of registration(NEWS and PROMO for instance), because the token is related to the app and the device. Is that correct?
The other thing that is not so clear to me is, if a device is registered for a specific notification is it possible to send the notification only to a set o devices?
If nothing of that is offered by Apple Push services do you think that is possible to manage everything like that:
-I register the app device for notification if (PROMO || NEWS) are selected
-I get the token
-I send the token to my server giving also as additional info about the service which the user wants to subscribe
-The server (provider) register the token and the kind of subscription (PROMO || NEWS)
-Later when I have a notification to push I ask the server all the tokens registered for that specific category and then I send the notification only to those devices registered for that category.
Thanks for helping me out I'm really confused.
"Reading the Apple doc, that was not so clear to me, it seems that once the app device is registered I receive just one token and seems impossible to receive more tokens for different kind of registration(NEWS and PROMO for instance), because the token is related to the app and the device. Is that correct?"
YES
The other thing that is not so clear to me is, if a device is registered for a specific notification is it possible to send the notification only to a set o devices?
YES, you need a DB where you connect a Push Token with the related Services (promo | news). If you have a new Promo Push Message you send the message to all related token. on the app site, everytime the user change the categorie (promo / news) you should prpvide these infos to your service with the push token.
These are all problems that you have to solve yourself on the server side. The push service simply provides a means to send a single message to a single device. You have to figure out yourself which messages you want to send to which devices. Each message has to be sent individually, there's no way to "broadcast" a message to all your users directly.
You could think of the push tokens as email addresses – of course, one email address might be subscribed to different newsletters from the same publisher, but it's the publisher's job (yours) to figure out whom to send which newsletters, not the email provider's (Apple's).
You should think of the push notification registration like my I send the user push notifications. Not what kind of push notification can I send the user.
Then you need to do serverside filtering on you categories, like the ones in your example promo and news.
The perferance of the user should be stored on your server, so you will know what kind of notification to send to which user.

Sending push notification from device to all contacts

I have an app where I would like to select a user in my contacts. If that person has my app installed, then I would like to send some data that is relevant for this app using a notification. The other user gets the notification, and acts on it within this app. Is that possible? I am thinking that if I can identify that users device, I can store this message in a server. When the user connects to the app, it will retrieve and get this users message from the server. But question is can I identify other devices like this, from my contact list. Can I send this notification using phone number instead of device id?
Please help.
Thank you,
Anks
No, you cannot. You have to have the Apple Push Token for a device and the user has to have notifications turned on to be able to receive notifications from Apple Push.
You would need to maintain a server side database linking the APNS Token to a specific user account. There are a lot of dots to connect with this kind of implmentation. Your better bet would be maintaining a "list of contacts" within your App to link contacts to.