https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
Em didFinishLaunchingWithOptions
:
- (BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes: types categories: nil ];
[[UIApplication sharedApplication ] registerUserNotificationSettings: mySettings];
[[UIApplication sharedApplication ] registerForRemoteNotifications ];
}
/* *
* Fetch and Format Device Token and Register Important Information to Server
*/
- (void )application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[devToken description ]
stringByReplacingOccurrencesOfString: @" <" withString: @" " ]
stringByReplacingOccurrencesOfString: @" >" withString: @" " ]
stringByReplacingOccurrencesOfString: @" " withString: @" " ];
/* * SAMPLE REQUEST **/
// Build URL String for Registration
NSString *URL = @" push.domain.com" ;
NSString *endpoint = @" /registerdevice" ;
NSString *data = [[NSString alloc ] initWithData: responseData encoding: NSUTF8StringEncoding];
data = [data stringByAppendingFormat: @" &device_token=%@ " , deviceToken];
NSInteger dataLength = [[data dataUsingEncoding: NSUTF8StringEncoding] length ];
NSString *dataLengthString = [NSString stringWithFormat: @" %d " , dataLength];
// Register the Device Data
NSURL *url = [[NSURL alloc ] initWithScheme: @" http" URL: URL path: endpoint];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL: url];
[request setHTTPMethod: @" POST" ];
[request setHTTPBody: [data dataUsingEncoding: NSUTF8StringEncoding]];
[request setValue: dataLengthString forHTTPHeaderField: @" Content-Length" ];
[request setValue: @" application/x-www-form-urlencoded" forHTTPHeaderField: @" Current-Type" ];
// Send request
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSLog (@" Register URL: %@ " , url);
NSLog (@" Return Data: %@ " , [NSString stringWithUTF8String: [returnData bytes ]]);
return (returnData != nil );
}
/* *
* Failed to Register for Remote Notifications
*/
- (void )application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog (@" Error in registration. Error: %@ " , error);
}
/* *
* Remote Notification Received while application was open.
*/
- (void )application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog (@" remote notification: %@ " ,[userInfo description ]);
NSDictionary *apsInfo = [userInfo objectForKey: @" aps" ];
// Play the specified sound
NSString *sound = [apsInfo objectForKey: @" sound" ];
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate );
// Change `App Bagde Number to the amount specified in the notification
application.applicationIconBadgeNumber = [[apsInfo objectForKey: @" badge" ] integerValue ];
}
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW2