Skip to content

Instantly share code, notes, and snippets.

@rajiv-singaseni
Created March 29, 2012 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajiv-singaseni/2235318 to your computer and use it in GitHub Desktop.
Save rajiv-singaseni/2235318 to your computer and use it in GitHub Desktop.
An iOS sample to send a toast notification to an Windows 8 device.
//
// RegistrationController.m
// iPhone
//
// Created by xcode4 on 20/03/2012.
// Copyright (c) 2012 WebileApps. All rights reserved.
//
#import "RegistrationController.h"
#import "ASIFormDataRequest.h"
@implementation RegistrationController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// [self fetchAccessToken];
[self sendPush];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(sendPush) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button setTitle:@"Send Push" forState:UIControlStateNormal];
button.frame = CGRectMake(100, 100, 120, 44);
}
- (void) fetchAccessToken {
NSURL *registrationURL = [NSURL URLWithString:@"https://login.live.com/accesstoken.srf"];
// ASIFormDataRequest *registrationRequest = [[ASIFormDataRequest alloc] initWithURL:registrationURL];
ASIHTTPRequest *registrationRequest = [[ASIHTTPRequest alloc] initWithURL:registrationURL];
[registrationRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[registrationRequest setRequestMethod:@"POST"];
NSString *postBody = @"grant_type=client_credentials&client_id=ms-app://s-1-15-2-2192266681-671918058-3069766759-3941978237-3589562082-3490105649-3234865011&client_secret=8FrewzBvCONvLNbSPDnPrgTq5cC3ydj3&scope=notify.windows.com";
[registrationRequest appendPostData:[postBody dataUsingEncoding:NSASCIIStringEncoding]];
// [registrationRequest setPostValue:@"client_credentials" forKey:@"grant_type"];
// [registrationRequest setPostValue:@"ms-app://s-1-15-2-2192266681-671918058-3069766759-3941978237-3589562082-3490105649-3234865011" forKey:@"client_id"];
// [registrationRequest setPostValue:@"8FrewzBvCONvLNbSPDnPrgTq5cC3ydj3" forKey:@"client_secret"];
// [registrationRequest setPostValue:@"notify.windows.com" forKey:@"scope"];
registrationRequest.delegate = self;
[registrationRequest startAsynchronous];
}
-(void) sendPush {
NSURL *notificationURL = [NSURL URLWithString:@"https://db3.notify.windows.com/?token=AQUAAAACOdUBkiMEPFmt0VqX%2fnLH9XTwtGdcZz7SlKfDJCVybPvR2GPRtgNeeB192P9icEKb5YyrwvCryLJvc%2bQVea%2fHqKxUixljU18aGZjdxZpHXGYUIL5GWjJj0ALwQeHsHsI%3d"];
ASIHTTPRequest *notificationRequest = [[ASIHTTPRequest alloc] initWithURL:notificationURL];
[notificationRequest setRequestMethod:@"POST"];
[notificationRequest addRequestHeader:@"Content-Type" value:@"text/xml"];
[notificationRequest addRequestHeader:@"Authorization" value:@"Bearer EgAeAQMAAAAEgAAACoAAx0ra9cDIpyvkNpqlCcx4LJ6thnmR/muqvSQMt54pLrZSg25gCEMEULbGkNcyfl35phHm6IQXceAu+JsnP3JzJSx4jbRTWdXto+DbizlGBk7mo+BqL7GFTnuI2npiry41HhKBJkNueFsoJXLENk7WRkJNeiUM4oQQjnW2rpz0MjmNAFoAjQAAAAAAIyoJSLoFdE+6BXRP60gEAA8AMTgzLjgyLjE3OS4yMTQAAAAAAF4AbXMtYXBwOi8vcy0xLTE1LTItMjE5MjI2NjY4MS02NzE5MTgwNTgtMzA2OTc2Njc1OS0zOTQxOTc4MjM3LTM1ODk1NjIwODItMzQ5MDEwNTY0OS0zMjM0ODY1MDExAA=="];
[notificationRequest addRequestHeader:@"X-WNS-TYPE" value:@"wns/toast"];//"X-WNS-Type", "wns/tile"
// [notificationRequest addRequestHeader:@"X-WNS-TYPE" value:@"wns/tile"];//"X-WNS-Type", "wns/tile"
[notificationRequest addRequestHeader:@"Host" value:@"cloud.notify.windows.com"];
// NSString *notificationXML = @"<tile><visual version='1' lang='en-US'><binding template='TileWideText03'><text id='1'>Hello World! My very own tile notification</text></binding><binding template='TileSquareText04'><text id='1'>Hello World! My very own tile notification</text></binding></visual></tile>";
NSString *notificationXML = @"<toast><visual version='1' lang='en-US'><binding template='ToastText02'><text id='1'>Heading text</text><text id='2'>Body text that wraps.</text></binding></visual></toast>";
[notificationRequest addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d",notificationXML.length]];
[notificationRequest addRequestHeader:@"X-WNS-RequestForStatus" value:@"true"];
[notificationRequest appendPostData:[notificationXML dataUsingEncoding:NSASCIIStringEncoding]];
notificationRequest.delegate = self;
[notificationRequest startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"response: %d, %@", request.responseStatusCode, request.responseString);
if(request.responseStatusCode == 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sent successfully" message:request.responseString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"response: %@", request.responseString);
NSLog(@"error: %@",[request.error.userInfo description]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed" message:[request.error.userInfo description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment