ios apple push-notification server side design - ios

Our app users can login their accounts and if a user is logged in, then he can receive push notifications when someone added him as a friend. Based on my understanding, our server needs to record the userID and its notification token, so that any events happened to the userID, the server can find its token and send to apple APN. If this token is changed, we also need to update the record in server. I am not quite sure if my understanding is correct or not. anyone can help? Thank you.

Yes, you have to store on your server a unique ID for each user and its notification token. You should update this token stored on your server with a connection in AppDelegate method "didFinishLaunchingWithOptions". Everytime something happens, as a new friend request, your server have to send a new push notification using the stored token.

Related

Login/Logout user with push notification issue

In my app successfully implement push notification related funda and working well.
But facing issue when user successfully logout and login/signup with new user did not get notification.
YES! Here issue is for DeviceToken.
First time I got deviceToken when register push notification.
Pass this deviceToken to server and and receive push notification.
Logout this user and register/login through another user
did not get any token try to unregisterForRemoteNotifications but not longer working with iOS 10. and might be not good idea to set like that.
If we can get new device token then issue will be resoled but might be not possible.
NOTE : By help backend side might be we can solve but suggest me better way. If possible at our side then It's cool.
So what can I do for my second user?
First of all you should Write your notification registration code in
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
method. Then save that token somewhere e.g. in user defaults
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSString *token; //extract from devToken
[[NSUserDefaults standardUserDefaults] setObject:token forKey:#"device_token"];
}
Now on successfull login send this token to your backend server. Also on logout send same token to backend in logout request or something and deregister this token from your backend system.
didRegisterForRemoteNotificationsWithDeviceToken is called once on successful registration of remote notification. You can persist the token as Rahul suggested. But instead of reading this value in didRegisterForRemoteNotificationsWithDeviceToken(), read it in a method that is called after the user logs in. If the value is present, send this token to your server. Your server should remove the token associated with the previous user and assign this token to the new user. From apple server/device perspective, a user is not associated with a device token. So either your api is not getting called after the new user logs in or there is a token mapping issue on your server side.
You can do one thing when the time of logout call one web service that will clear your device token for that particular user so when user will register again it will registered with device token and on log out it will clear device token from database

Swift: How to get the Registration Token of Google Cloud Messaging

I'm trying to implement google cloud messaging in one of my project, where a type-A user submit something, it will push the information to the server and it will send to a type-B user. If the type-B user agree the things that the type-A user sent, they would say yes and it will send that to server and return the information to the type-A user.
That's the idea. So, I follow the example at https://github.com/googlesamples/google-services/tree/master/ios/gcm, however I can only get the registration token printed on the output. If I want to get/retrieve the registration token to pass to the server, how could I achieve it?
In here, I've got the registration token called from AppDelegate and printed out, how can I return/get it outside of the AppDelegate?
Also, how could I notify the user when they turn off the screen? (send notification) Right now, I only receive notification from the input)
Thank you for reading the question. Any help/suggestion would be greatly appreciated.

iOS APNS and device token good practices

I'm making use of push notifications in my iOS app, and it is an app which requires users to log in to access, so of course there is a logout function as well. I'd like to know if it is a good practice, or if it is the common thing to do, to "clear" the device token sent to your provider when a user logs out (I mean, to send an empty string as the device token).
Thanks in advance
1)
Send empty String to Provider and keep device token saved locally to access on Login.
OR
2)
Your logout service should set flag against current user and device token to disable pushnotifications and enable on login again.

Obtain NEW Apple device token?

I am creating an app that allows the user to log out of the app and log in as different user if they want to. Is there any way to obtain a new device token from Apple once the new user logs in? Is there a way to force call the didRegisterForRemoteNotificationsWithDeviceToken method?
No, you can't request a new device token. They expire from time to time, and only then will you get a new one (or if you have a different app with a different bundle id, the token will be different).
Create a function to handle didRegister and call that from didRegisterForRemoteNotificationsWithDeviceToken. Then use that function when you need to force the call.
Since users are logging in, pass the information with the device token to the server every time someone logs in and associate the user to the token on the server side.
There is no way to get a different device token. You need to remove the token from the backend when the user logs out.
I'm not sure how it behaves in iOS7 and later, but prior to iOS7, all applications on the same device would get the same device token, and therefore what you ask is impossible. As of iOS7, each application gets its own device token, but I'm not sure if that token can change on consecutive registrations.
You can always force call the didRegisterForRemoteNotificationsWithDeviceToken by calling registerForRemoteNotificationTypes, but usually you'll get the same device token.
If the goal is to associate notifications with specific users, then as of iOS 10, you can try implementing a NotificationService extension to filter only those notifications associated with the currently authenticated user. While multiple users on the same device will be associated with the same device token at the server, the client will only display notifications for the current user - assuming that only one user at a time can be logged-in.
This approach also allows for having notifications that don't require authentication - just pass those through unaltered.
As mentioned above, you can force a request-response token update after user login by explicitly calling UIApplication.shared.registerForRemoteNotification (Swift 3+). Then send that user+device token combination to your application server for use.

Ruby on Rails with Faye, how to broadcast data to specific subcsriber

i want to build a Ruby on Rails web-apps where using facebook style notification, for the push server i'm using FAYE, is there any solution to broadcast notification for specific client, for case studies in Facebook only your account have notification if somebody send you a message or tag your photo. Thanks
In your client side JavaScript, subscribe to a channel name based on the user (e.g. '/messages/userid_1234') and use that to send private messages from the server.
For security reasons, don't use the actual user ID or even a hash of the username or email, since someone could spoof a subscribe that way. Generate a random GUID on signup for each user and store that with your user's account details.

Resources