Skip to content

Instantly share code, notes, and snippets.

@igor9silva
Created May 4, 2016 13:10
Show Gist options
  • Save igor9silva/9052029c5f54a6430a5b6c37beac7714 to your computer and use it in GitHub Desktop.
Save igor9silva/9052029c5f54a6430a5b6c37beac7714 to your computer and use it in GitHub Desktop.

Registro do device

Docs

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

Implementação

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];
}
Callbacks
/**
 * 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];
}

Disparo da notificação

Docs

APNS Request

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW1

JSON Structure

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment