Created
July 5, 2017 07:10
-
-
Save andreas-nesheim/9dd230891411d9c431bdbe0b4adc2cce to your computer and use it in GitHub Desktop.
This file contains 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
using Capgemini.Mobile.Framework; | |
using Firebase.Analytics; | |
using Firebase.CloudMessaging; | |
using Foundation; | |
using HockeyApp.iOS; | |
using Njord.Iphone.Utils; | |
using Njord.Mobile.Common._Framework.Interceptors; | |
using Njord.Mobile.Common._Framework.Messaging; | |
using Njord.Mobile.Common.DomainServices; | |
using Njord.Mobile.Common.Interfaces.Logging; | |
using Njord.Mobile.Common.Interfaces.Messaging; | |
using Njord.Mobile.Common.Interfaces.Proxy; | |
using Njord.Mobile.Common.Interfaces.Services; | |
using Njord.Mobile.Common.Model.Pocos.Services; | |
using Njord.Mobile.Common.Services; | |
using Njord.Mobile.Common.Services.Login; | |
using System; | |
using System.Diagnostics; | |
using System.Globalization; | |
using System.Net; | |
using System.ServiceModel; | |
using UIKit; | |
using UserNotifications; | |
namespace Njord.Iphone | |
{ | |
[Register("AppDelegate")] | |
public class AppDelegate : UIApplicationDelegate, IMessagingDelegate | |
{ | |
#if DEBUG || TEST | |
private const string HockeyAppAppid = "4595ed2870bc4621ae6ccc9a0718d836"; // Njord Test | |
private const string EndpointAddress = @"https://test.shiprep.no/NjordMobileServiceDev/NjordMobileService.svc"; // Dev | |
//private const string EndpointAddress = @"https://test.shiprep.no/NjordMobileServiceSys/NjordMobileService.svc"; // SYS | |
//private const string EndpointAddress = @"https://153.44.5.234/NjordMobileService/NjordMobileService.svc"; // Test | |
#else | |
//private const string EndpointAddress = @"https://www.shiprep.no/NjordMobileService2017/NjordMobileService.svc"; // Prod | |
private const string EndpointAddress = @"https://www.shiprep.no/NjordMobileServiceBeta/NjordMobileService.svc"; // Beta | |
private const string HockeyAppAppid = "6fb8133132ac443ea42ff5bc5dc976b8"; // Beta | |
//private const string HockeyAppAppid = "1162e96de07d4636aaaf3c8fa0cb1384"; // Prod | |
#endif | |
public override UIWindow Window { get; set; } | |
[Preserve(typeof(UIBarButtonItem))] | |
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) | |
{ | |
SetDebugRules(); | |
#if DEBUG || TEST | |
SetTestColors(); | |
#endif | |
RegisterAllComponents(); | |
SetupMessageBus(); | |
SetNorwegianCulture(); | |
SetupHockeyapp(); | |
//Xamarin.Calabash.Start(); | |
App.Configure(); | |
RegisterForRemoteNotifications(); | |
ConnectToFirebaseMessagingService(); | |
RegisterPushNotificationCategory(); | |
//var token = InstanceId.SharedInstance.Token; | |
return true; | |
} | |
private void RegisterForRemoteNotifications() | |
{ | |
// Register your app for remote notifications. | |
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) | |
{ | |
// iOS 10 or later | |
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound; | |
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => | |
{ | |
Console.WriteLine(granted); | |
}); | |
// For iOS 10 display notification (sent via APNS) | |
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); | |
// For iOS 10 data message (sent via FCM) | |
Messaging.SharedInstance.RemoteMessageDelegate = this; | |
} | |
else | |
{ | |
// iOS 9 or before | |
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; | |
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null); | |
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); | |
} | |
UIApplication.SharedApplication.RegisterForRemoteNotifications(); | |
} | |
private static void ConnectToFirebaseMessagingService() | |
{ | |
Messaging.SharedInstance.Connect(error => | |
{ | |
if (error != null) | |
{ | |
// Handle if something went wrong while connecting | |
} | |
else | |
{ | |
// Let the user know that connection was successful | |
} | |
}); | |
} | |
private static void RegisterPushNotificationCategory() | |
{ | |
var actionId = "accept"; | |
var title = "Godta"; | |
var actionAccept = UNNotificationAction.FromIdentifier(actionId, title, UNNotificationActionOptions.None); | |
actionId = "decline"; | |
title = "Avvis"; | |
var actionDecline = UNNotificationAction.FromIdentifier(actionId, title, UNNotificationActionOptions.None); | |
// Create category | |
var categoryID = "assignedPilotage"; | |
var actions = new UNNotificationAction[] { actionAccept, actionDecline }; | |
var intentIDs = new string[] { }; | |
var categoryOptions = new UNNotificationCategoryOptions[] { }; | |
var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None); | |
// Register category | |
var categories = new UNNotificationCategory[] { category }; | |
UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories)); | |
} | |
private void SetDebugRules() | |
{ | |
AppDomain.CurrentDomain.UnhandledException += (sender, e) => | |
{ | |
var ex = e.ExceptionObject as Exception; | |
if (null == ex) return; | |
Debug.WriteLine(ex.Message); | |
Debug.WriteLine(ex.StackTrace); | |
Debugger.Break(); | |
}; | |
} | |
private void SetupHockeyapp() | |
{ | |
var manager = BITHockeyManager.SharedHockeyManager; | |
manager.Configure(HockeyAppAppid); | |
manager.UserId = IoC.Get<ISettingsService>().Get(SettingConstant.PhoneNumber); | |
manager.StartManager(); | |
} | |
private void SetupMessageBus() | |
{ | |
IoC.Get<IMessageBus>().Subscribe(this); | |
} | |
private void SetTestColors() | |
{ | |
UINavigationBar.Appearance.TintColor = UIColor.Magenta; | |
UITabBar.Appearance.TintColor = UIColor.Magenta; | |
} | |
private void SetNorwegianCulture() | |
{ | |
var culture = new CultureInfo("nb-NO"); | |
CultureInfo.DefaultThreadCurrentCulture = culture; | |
CultureInfo.DefaultThreadCurrentUICulture = culture; | |
} | |
private void RegisterAllComponents() | |
{ | |
IoC.Reset(); | |
IoC.RegisterSingleton<IMessageBus, MessageBus>(); | |
IoC.RegisterSingleton<ILog, DebugWriteLineLog>(); | |
IoC.RegisterSingleton<INjordServiceProxy, NjordServiceProxy>(); | |
IoC.RegisterSingleton<ISettingsService, IphoneSettings>(); | |
IoC.RegisterFactory<NjordMobileService>(() => new NjordMobileServiceClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), new EndpointAddress(EndpointAddress))); | |
IoC.RegisterTransient<INetworkAvailabilityService, IphoneNetworkAvailability>(); | |
IoC.RegisterSingleton<IEndpoint, EndPointService>(); | |
IoC.RegisterInstance<IDispatchOnUiThread>(new IphoneDispatcher(InvokeOnMainThread)); | |
IoC.RegisterInterceptor<AutoSubscriberInterceptor>(); | |
IoC.RegisterSingleton<IDeviceInfoService, IphoneDeviceInfoService>(); | |
IoC.RegisterSingleton<IVersionService, VersionService>(); | |
IoC.RegisterSingleton<IAddPilotageLocation, PilotageLocationService>(); | |
IoC.RegisterSingleton<IRemovePilotageLocation, PilotageLocationService>(); | |
IoC.RegisterSingleton<ICalendarService, CalendarService>(); | |
IoC.RegisterSingleton<ILogoffService, LogoffService>(); | |
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; | |
IoC.Get<ILog>().Info("AppDelegate", "Container configured."); | |
} | |
public override void OnResignActivation(UIApplication application) | |
{ | |
// Invoked when the application is about to move from active to inactive state. | |
// This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) | |
// or when the user quits the application and it begins the transition to the background state. | |
// Games should use this method to pause the game. | |
} | |
public override void DidEnterBackground(UIApplication application) | |
{ | |
// Use this method to release shared resources, save user data, invalidate timers and store the application state. | |
// If your application supports background exection this method is called instead of WillTerminate when the user quits. | |
Messaging.SharedInstance.Disconnect(); | |
Console.WriteLine("Disconnected from FCM"); | |
} | |
public override void WillEnterForeground(UIApplication application) | |
{ | |
// Called as part of the transiton from background to active state. | |
// Here you can undo many of the changes made on entering the background. | |
} | |
public override void OnActivated(UIApplication application) | |
{ | |
// Restart any tasks that were paused (or not yet started) while the application was inactive. | |
// If the application was previously in the background, optionally refresh the user interface. | |
ConnectToFirebaseMessagingService(); | |
} | |
public override void WillTerminate(UIApplication application) | |
{ | |
// Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground. | |
} | |
// Receive data message on iOS 10 devices. | |
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage) | |
{ | |
Console.WriteLine(remoteMessage.AppData); | |
} | |
// To receive notifications in foregroung on iOS 9 and below. | |
// To receive notifications in background in any iOS version | |
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) | |
{ | |
// If you are receiving a notification message while your app is in the background, | |
// this callback will not be fired 'till the user taps on the notification launching the application. | |
// Do your magic to handle the notification data | |
Console.WriteLine(userInfo); | |
} | |
} | |
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate | |
{ | |
public UserNotificationCenterDelegate() | |
{ | |
} | |
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) | |
{ | |
// Do something with the notification | |
Console.WriteLine("Active Notification: {0}", notification); | |
// Tell system to display the notification anyway or use | |
// `None` to say we have handled the display locally. | |
completionHandler(UNNotificationPresentationOptions.Alert); | |
} | |
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) | |
{ | |
Console.WriteLine("Did receive notificaton called!"); | |
switch (response.ActionIdentifier) | |
{ | |
case "accept": | |
Console.WriteLine("Accepted clicked!"); | |
SendAcceptedPilotageReceipt(); | |
break; | |
default: | |
// Take action based on identifier | |
if (response.IsDefaultAction) | |
{ | |
// Handle default action... | |
} | |
else if (response.IsDismissAction) | |
{ | |
// Handle dismiss action | |
} | |
Console.WriteLine("Default stuff clicked!"); | |
break; | |
} | |
completionHandler(); | |
} | |
private void SendAcceptedPilotageReceipt() | |
{ | |
UIApplication.SharedApplication.OpenUrl(new NSUrl("https://www.dagbladet.no")); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment