Skip to content

Instantly share code, notes, and snippets.

@witoong623
Last active April 27, 2017 05:59
Show Gist options
  • Save witoong623/5f1a717653ac7ca436f9913fff69e85c to your computer and use it in GitHub Desktop.
Save witoong623/5f1a717653ac7ca436f9913fff69e85c to your computer and use it in GitHub Desktop.

Mobile Application version checking and Notification sender

Global API

public class GenericResponseMessage
{
    // indicates result of HTTP API call, 200 = success, 400 = parameter invalid, 500 = internal server error
    public int status { get; set; }
    // indicates error messages, otherwise null
    public string[] message { get; set; }
}

This class is JSON return from controller's action when response to POST request

Notification sender

Available API

public class RegisteredTokenRepository
{
    public RegisteredTokenRepository(string connectionString);
    
    public async Task RegisterUserTokenAsync(int id, string token);
    public async Task<string> GetRegisteredTokenAsync(int emp_id);
    public async Task UpdateRegisteredTokenAsync(int id, string newToken);
    public async Task DeRegisteredTokenAsync(int id);
}

When one wants to associate mobile token with user.
await RegisterUserTokenAsync(int id, string token);
Return Task. throw InvalidOperationException when Token already exist in system.
Probably throws SQLiteException.
Behavior: this method will delete existing token that associated with user and insert new token.

When one wants to deregister mobile token with user.
await DeRegisteredTokenAsync(int id);
Return Task. Probably throw SQLiteException.

When one wants to update registered token that already associated with user.
await UpdateRegisteredTokenAsync(int id, string newToken);
Return Task. Probably throw SQLiteException.
Behavior: this method will delete existing token and insert new token.

When one wants to get mobile token.
string token = await GetRegisteredTokenAsync(int id);
Return token string, otherwise null. Probably throw SQLiteException.

public class MobileNotificationSender
{
    public MobileNotificationSender(string serverKey, RegisteredTokenRepository tokenRepo);
    public RegisteredTokenRepository TokenRepository { get; }
    public async Task SendNotification(int id, string title, string body);
}

When one wants to send notification to mobile application.
await SendNotification(int id, string title, string body);
Return Task. throw ArgumentException when parameter invalid. Probably throw SQLiteException
Behavior: Ignores every error from FCM except Unauthorize, FCMException(500 error code). Will update existing token in database to new token if FCM response specify.

Mobile Application version checking and Notification sender

Global API

public class GenericResponseMessage
{
    // indicates result of HTTP API call, 200 = success, 400 = parameter invalid, 500 = internal server error
    public int status { get; set; }
    // indicates error messages, otherwise null
    public string[] message { get; set; }
}

This class is JSON return from controller's action when response to POST request

Mobile Application version checking

Available API

public class AndroidCurrentVersion
{
    public int VersionCode { get; set; }
    public string VersionName { get; set; }
    public string DownloadLink { get; set; }
    public string FileName { get; set; }
    public bool IsSkipable { get; set; }
}
public class iOSCurrentVersion
{
    public string VersionName { get; set; }
    public string DownloadLink { get; set; }
    public string FileName { get; set; }
    public bool IsSkipable { get; set; }
}
public class MobileAppVersionManager
{
    public MobileAppVersionManager(string connectionString);
    public async Task<AndroidCurrentVersion> GetCurrentAndroidVersionAsync();
    public async Task<iOSCurrentVersion> GetCurrentiOSVersionAsync();
    public async Task SetCurrentAndroidVersionAsync(AndroidCurrentVersion newVersion);
    public async Task SetCurrentiOSVersionAsync(iOSCurrentVersion newVersion);
}

MobileAppVersionManager should be use as singleton.
When one wants to set current version of Application.
await MobileAppVersionManager.Current.SetCurrentAndroidVersionAsync(AndroidCurrentVersion);
await MobileAppVersionManager.Current.SetCurrentiOSVersionAsync(iOSCurrentVersion);
These methods return Task. Exception is throw when error occours.
ArgumentException when encouter invalid parameter. SQLiteException

When one wants to get current version .
await MobileAppVersionManager.Current.GetCurrentAndroidVersionAsync()
await MobileAppVersionManager.Current.GetCurrentiOSVersionAsync()
These methods return AndroidCurrentVersion and iOSCurrentVersion respectively.
Throw exception probably throw SQLiteException.

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