Last active
August 29, 2015 14:05
-
-
Save ChrisRisner/a6a108ec71ae31705fa7 to your computer and use it in GitHub Desktop.
iOS Push Notifications with Notification Hubs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; | |
return YES; | |
} | |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
NSLog(@"APN device token: %@", deviceToken); | |
NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken]; | |
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@"<" withString:@""]; | |
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@">" withString:@""]; | |
if (self.vc) { | |
[self.vc setPushToken:deviceTokenString andData:deviceToken]; | |
} | |
} | |
- (void)application:(UIApplication *)application | |
didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
NSLog(@"%@", userInfo); | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message: | |
[[userInfo objectForKey:@"aps"] valueForKey:@"alert"] delegate:nil cancelButtonTitle: | |
@"OK" otherButtonTitles:nil, nil]; | |
[alert show]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
notificationHubService.apns.send(null, | |
'{"aps":{"alert" : "Hello from Mobile Services!"}}' | |
, | |
function (error) | |
{ | |
if (!error) { | |
console.warn("Notification successful"); | |
} | |
} | |
); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)tappedRegisterWithTemplates:(id)sender { | |
NSArray *tagArray = | |
@[@"MyTag", | |
@"AllUsers", | |
@"iOSUser" | |
]; | |
NSSet *tagSet = [[NSSet alloc] initWithArray:tagArray]; | |
NSString *template = @"{\"aps\": {\"alert\": \"$(message)\"}}"; | |
[self.hub registerTemplateWithDeviceToken:self.pushTokenData name:@"messageTemplate" jsonBodyTemplate:template expiryTemplate:nil tags:tagSet completion:^(NSError *error) { | |
if (error != nil) { | |
NSLog(@"Error registering for push notifications: %@", error); | |
} else { | |
NSLog(@"Success registering for push with template"); | |
} | |
}]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
notificationHubService.apns.send('MyTag', | |
{ | |
alert: "Hello MyTag", | |
payload: { | |
inAppMessage: "Hello MyTag!" | |
} | |
} | |
, | |
function (error) | |
{ | |
if (!error) { | |
console.warn("Notification successful"); | |
} | |
} | |
); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.get = function(request, response) { | |
var azure = require('azure'); | |
var notificationHubService = azure.createNotificationHubService('NotificationHubName', | |
'NotificationHubFullSharedAccessSignature'); | |
var payload = '{ "message" : "Template push to everyone!"}'; | |
notificationHubService.send(null, payload, | |
function(error, outcome) { | |
console.log('issue sending push'); | |
console.log('error: ', error); | |
console.log('outcome: ',outcome); | |
}); | |
response.send(statusCodes.OK, { message : 'Notification Sent' }); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
AppDelegate *delegate = [UIApplication sharedApplication].delegate; | |
delegate.vc = self; | |
self.hub = [[SBNotificationHub alloc] initWithConnectionString: | |
@"NotificationHubListenSharedAccessSignature" | |
notificationHubPath:@"NotificationHubName"]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment