Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Push Notifications to specific users?

Is possible to send an iOS push notification to a specific device? I've built a forum type app which the user can create a question and other people can answer it. I need to send an iOS Push Notification to the specific user which asked the question informing them that the question was answered. Is this possible to be done through PHP or another method?

like image 998
Mateus Avatar asked May 02 '12 21:05

Mateus


1 Answers

Yes, you can absolutely send a push notification to a specific device.

First you need to ask the device for permission to receive push notifications:

[[UIApplication sharedApplication]
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                     UIRemoteNotificationTypeSound |
                                     UIRemoteNotificationTypeAlert)]; 

Then if the user accepts you will receive the delegate message didRegisterForRemoteNotificationsWithDeviceToken. You then pass that deviceToken to your server with the user Id from your app that identifies the user in your forum. Something like this:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {

    NSString* deviceToken = [[[[_deviceToken description]
                            stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                            stringByReplacingOccurrencesOfString: @">" withString: @""] 
                            stringByReplacingOccurrencesOfString: @" " withString: @""];

   //send deviceToken and your userId to your server to associate the two together 
}

Then you need to build your push server which sends the notifications, which I won't get into here as there is a ton of documentation online for that and it is quite involved. You can also use third party services for this such as Urban Airship and StackMob.

like image 89
Joel Avatar answered Oct 02 '22 04:10

Joel