How do push notifications on the iPhone determine which users are notified? - iphone

I'm just trying to understand push notification more.
Let's say I have a web service that my app connects to that needs a user to sign in with a username and password. And this all works fine when running the app.
Now... how does the web service determine which user to send the push notification to?
I'm really unsure on how it works with users with usernames and passwords. It would be great if someone could enlighten me to this. A better understanding could improve my apps in the future. Thanks!

I'll give a brief over view of how push notifications work:
First, your app will call the registerForRemoteNotificationTypes: method. This will check that your app is allowed to send and receive the types of notifications you requested, and contact Apple's servers to register your device. You will get back a special token used to uniquely identify the device.
Your app needs to send this token to your server along with the details of the account that the user is logged in with, so your server can associate that token with the user.
When you want to send a push notification to a user, you lookup the token you received previously for that user and use that when sending the notification to Apple's server. This will forward the notification to the appropriate device.
It's probably a good idea if you read the Apple documentation here: Apple Push Notification Service. This will give you a clearer understand of how it all works.

Here is the wonderful tutorial explains in detail, http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

For login procedures, you don't even need push notifications. If you need a general introduction, you will find one here

Below I have mentioned 2 links.
https://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html
http://mobiforge.com/developing/story/programming-apple-push-notification-services
Now coming to your question, push notification is pushed from a server. Now for a particular user the server side should maintain the database such that whatever changes made to a specific user should be pushed to that device id.
Hope this help.

Push notification is awesome feature of iOS apps. It works like your application needs to register for Push notification as
//Your application registeres for push notification using following line
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
This will tells you application to handle generation of device token failure/success by delegate methods
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
//Your application registered for push notification i.e. allowed by user.
//You need to take device token and pass it to your webservice and store it in database.
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
//Your application registered for push notification i.e. not allowed by user.
}
So, Now what after you have device token ? .. Its simple now use it for sending push notification. You can find different code for that using PHP as well as .Net. Search around and you will get plenty of them.

Related

How to determine Push notification status programmatically?

In my app, for first time push notification registration, I call didRegisterForRemoteNotificationsWithDeviceToken and save the device token in persistence as well as update my server list for device token. Now afterwards if somebody turns the push notification settings off from iPhone Settings how can I determine it from my app so that I can remove the device token from server as well. I know APNS provides a feedback list, but other than that is there a way to determine it in App programmatically? Thanks for any help!
I believe you do not want to manage tokens this way.
Your app should always be asking Apple for an APNs token. You should always then send that token to your own server, likely associating the token with your user (if you have one). You do this because the token could change, so you want to make sure you always have up-to-date tokens.
The Feedback service will tell you (actually, you poll it at some interval of your choosing) which tokens have become invalid. At this point, you remove the tokens from your server-side database. To be clear, you need a server-side process that polls Apple's feedback service and then updates your server-side database.
You will not receive feedback about invalid tokens until you try to send a notification using the token. The notification will (I believe) be accepted by Apple when you send it, but when Apple discovers it's for an invalid token, the message is dropped, and the token is added to your feedback.
Now, if the user of your app accepts push notifications when your app first asks about it, but later turns off notifications via the Settings app for your app, you will not get any feedback about it. What happens, near as I can tell, is that any notification you send to that device will be sent to the device, but the OS drops it, honoring the user's ultimate choice in the Settings app for your app and notifications.
Finally, there is an API you can call in your app to get a bitmask of which kinds of notifications are enabled for your app on the device. Here's a method I wrote for this purpose; adjust as needed:
+(BOOL)acceptsPushNotifications
{
UIRemoteNotificationType mask = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (mask & UIRemoteNotificationTypeAlert) == UIRemoteNotificationTypeAlert;
}
But I would not recommend using this to decide if you app should tell your server to delete the token from your database. That's not how the whole APNs system is intended to work... I believe.

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.

Apple Push Notifications to specific Users

as far as I understand APN's I can only send them to the app not a specific user that uses my app.
Is there a way of sending APN's only to specific users that use my app? I can't think of a way of doing this...
Greetz
APNS is not a broadcast medium. As it says in the documentation:
Apple Push Notification service transports and routes a notification
from a given provider to a given device.
When you send a notification from the server, one of the paramters is the device ID.
Apple's Push Notifications are always sent to specific 'users' (a specific device being an iphone, ipad)
What you do when you want to use APN is register the application for push notifications. Then you get a token that links the user's device to the notification service. You then use this token to 'push notifications' to APN which will in turn send a notification to that device. Notifications are pushed by 'Providers'.
Here's apple's documentation on the matter: Apple Push Notifications
Look up 'Registering for Remote Notifications' to register the device
and 'Handling Local and Remote Notifications' to handle the incoming notifications.
Read up on how to send notifications here: Providers
It is possible. I use Easy APNS and in newMessage() you can specify the PID (saved in a MySQL database) of the user.
Have a look at http://www.easyapns.com/

iOS - Push Notification: does it always display a notification

I'd like to know if it is possible to send a push notification, through APNS, without displaying a notification if the application isn't started? (in this case, the message received will be thrown away)
Thx
I may be misunderstanding your question, but I am assuming you are asking how to provide the user with options to turn off particular aspects of the push notification?
You will have to set this in the App. That is, if the user wishes to turn off this particular aspect, your App will have to send up the request for them.
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
So, if you still wanted to do badges, but no longer show alerts:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
Don't forget to resend the Token to your provider as it may have changed after this request.

access user push notification preferences/permissions in the iPhone using objective c

we are looking for a way to find out (not modify, just find out) if our application has been allowed to receive notifications or not from our service. This probably requires read-only access to the "general settings" properties or some specific API's. Does anybody know how to get that information? I am talking about the info in iPhone --> Settings --> Notifications
For those who are interested, this is the rationale:
we are having some issues with users pressing NO when asked to allow our application to receive push notifications. As you can imagine, users might just press no because they are racing through registration or because they don't quite understand what is happening, or simply because they are not too sure whether they should allow the application to get notifications. But then they forget they denied permission so file support requests because they don't receive push notifications.
Call -[[UIApplication sharedApplication] enabledRemoteNotificationTypes]. From the docs:
The values in the returned bit mask indicate the types of notifications currently enabled for the application. These types are first set when the application calls the registerForRemoteNotificationTypes: method to register itself with Apple Push Notification Service. Thereafter, the user may modify these accepted notification types in the Notifications preference of the Settings application. This method returns those initial or modified values.
-enabledRemoteNotificationTypes is deprecated.
It is recommended that you use the simple boolean instead:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]