Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active August 29, 2015 14:05
Show Gist options
  • Save ChrisRisner/a6a108ec71ae31705fa7 to your computer and use it in GitHub Desktop.
Save ChrisRisner/a6a108ec71ae31705fa7 to your computer and use it in GitHub Desktop.
iOS Push Notifications with Notification Hubs
- (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];
}
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' });
};
- (IBAction)tappedRegisterWithNoTags:(id)sender {
[self.hub registerNativeWithDeviceToken:self.pushTokenData tags:nil completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error registering for notifications: %@", error);
} else {
NSLog(@"Success registering for push with NO tags");
}
}];
}
- (IBAction)tappedRegisterWithTags:(id)sender {
NSArray *tagArray =
@[@"MyTag",
@"AllUsers",
@"iOSUser"
];
NSSet *tagSet = [[NSSet alloc] initWithArray:tagArray];
[self.hub registerNativeWithDeviceToken:self.pushTokenData tags:tagSet completion:^(NSError* error) {
if (error != nil) {
NSLog(@"Error registering for notifications: %@", error);
} else {
NSLog(@"Success registering for push with tags");
}
}];
}
- (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");
}
}];
}
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' });
};
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' });
};
- (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