Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active August 29, 2015 13:59
Show Gist options
  • Save ChrisRisner/10752731 to your computer and use it in GitHub Desktop.
Save ChrisRisner/10752731 to your computer and use it in GitHub Desktop.
Build 2013 Code gists
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
public async Task<bool> Authenticate(String username, object uiObject)
{
try
{
if (MobileService.CurrentUser != null &&
!string.IsNullOrEmpty(MobileService.CurrentUser.UserId))
return true;
this.Username = username;
ServiceHelper.MobileService.CurrentUser = await
PlatformSpecific.GetInstance().Authenticate(MobileService, uiObject);
RegisterWithNotificationHubs();
return true;
}
catch (Exception ex)
{
PlatformSpecific.GetInstance().LogInfo("Error authenticating: " + ex.Message);
return false;
}
}
exports.get = function(request, response) {
var httpRequest = require('request');
var url = 'https://chatdemo.servicebus.windows.net/webapi/contacts/';
httpRequest.get({
url: url
}, function(err, contactResponse, body) {
if (err) {
request.respond(statusCodes.INTERNAL_SERVER_ERROR,
'Unable to get contacts.');
} else if (response.statusCode !== 200) {
request.respond(statusCodes.BAD_REQUEST,
'Unaable to get contacts');
} else {
console.log('body:', body);
response.send(statusCodes.OK, body);
}
});
};
public class iOSSpecific : PlatformSpecific
{
public iOSSpecific()
{ this.Platform = "iOS"; }
public override void LogPlatform()
{ Console.WriteLine("Plaftorm is " + this.Platform); }
public override void LogInfo(string info)
{ Console.WriteLine("Info: " + info); }
public override async Task<MobileServiceUser> Authenticate(object msclient, object uiObject)
{
MobileServiceClient client = (MobileServiceClient)msclient;
return await client.LoginAsync((UIViewController)uiObject,
MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory);
}
}
function insert(item, user, request) {
request.execute({ success: function() {
request.respond(200, item);
var azure = require('azure');
var hub = azure.createNotificationHubService(process.env.NOTIFICATION_HUB_NAME,
process.env.NOTIFICATION_HUB_FULL_ACCESS_SIGNATURE);
var payload = { message: 'New message: ' + item.Text};
var tags = '';
if (item.Recipient !== '')
tags = item.Recipient;
else
tags = 'AllUsers';
console.log('Payload: ', payload);
console.log('Tags: ', tags);
hub.send(tags, payload,
function(error, outcome) {
console.log('issue sending push');
console.log('error: ', error);
console.log('outcome: ',outcome);
});
}, error: function(err) {
request.respond(500, "There was an error");
}});
}
public abstract class PlatformSpecific
{
private static PlatformSpecific ThisPlatform;
public static PlatformSpecific GetInstance()
{ return ThisPlatform; }
public static void SetPlatform(PlatformSpecific platform)
{ ThisPlatform = platform; }
public PlatformSpecific() {}
public string Platform { get; set; }
private string getPlatform()
{ return this.Platform; }
public abstract void LogPlatform();
public abstract void LogInfo(string info);
public abstract Task<MobileServiceUser> Authenticate(object msclient, object uiObject);
}
exports.post = function(request, response) {
var pushIdentifier = request.body.PushIdentifier;
var username = request.body.Username;
var platform = request.body.Platform;
var installationId = request.header('X-ZUMO-INSTALLATION-ID');
var azure = require('azure');
var hub = azure.createNotificationHubService(process.env.NOTIFICATION_HUB_NAME,
process.env.NOTIFICATION_HUB_FULL_ACCESS_SIGNATURE);
var registrationComplete = function(error, registration) {
if (!error) {
response.send(200, { Message: 'Registered' });
} else {
console.log('registration failed');
response.send(500, { Message: 'Registration failed!' });
}
}
// Function called to log errors.
var logErrors = function(error) {
if (error) {
console.error(error)
}
}
hub.listRegistrationsByTag(installationId, function(error, existingRegs) {
if (!error) {
if (existingRegs.length > 0) {
for (var i = 0; i < existingRegs.length; i++) {
hub.deleteRegistration(existingRegs[i].RegistrationId, logErrors);
}
}
//Register
if (platform == 'Android') {
var template = {
data: {
message: '$(message)'
}
};
hub.gcm.createTemplateRegistration(pushIdentifier, [username, platform, 'AllUsers', installationId],
template, registrationComplete);
} else if (platform == 'iOS') {
var template = {
alert: '$(message)'
};
hub.apns.createTemplateRegistration(pushIdentifier, [username, platform, 'AllUsers', installationId],
template, registrationComplete);
} else if (platform == "WinPhone") {
var template = {
text1: 'Ad',
text2: '$(message)'
};
hub.mpns.createToastRegistration(pushIdentifier,
[username, platform, 'AllUsers', installationId], template, registrationComplete);
} else if (platform == "WinStore") {
var template = {
text1: '$(message)'
};
hub.wns.createToastText01Registration(pushIdentifier,
[username, platform, 'AllUsers', installationId], template, registrationComplete);
}
} else {
console.error('Issue getting registrations');
response.send(500, { Message: 'Registration failed! 1'});
}
});
};
private async void RegisterWithNotificationHubs()
{
if (!String.IsNullOrEmpty(this.PushIdentifier))
{
NotificationHubRegistration registration = new NotificationHubRegistration()
{
Platform = PlatformSpecific.GetInstance().Platform,
PushIdentifier = this.PushIdentifier,
Username = this.Username
};
var response = await MobileService.InvokeApiAsync<NotificationHubRegistration, ApiResponse>
("RegisterforPush", registration);
if (response.Message == "Registered")
{
PlatformSpecific.GetInstance().LogInfo("Registered with Notification Hubs");
}
else
{
PlatformSpecific.GetInstance().LogInfo("Issue registering for Notification Hubs");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment