using System;
|
using AVFoundation;
|
using com.hdl.on;
|
using CoreLocation;
|
using Foundation;
|
|
using UIKit;
|
|
|
namespace Shared
|
{
|
public class BaseApplicationDelegate : UIApplicationDelegate
|
{
|
public static Action<UIApplication, NSDictionary> FinishedLaunchingAction;
|
public static Action<UIApplication> OnResignActivationAction;
|
public static Action<UIApplication> DidEnterBackgroundAction;
|
public static Action<UIApplication> WillEnterForegroundAction;
|
public static Action<UIApplication> OnActivatedAction;
|
public static Action<UIApplication> WillTerminateAction;
|
public static Action<string> DeviceTokenAction;
|
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
|
{
|
registerForRemoteNotification(application);
|
var musicInfo = new MusicInfo { };
|
Volume.Init();
|
|
FinishedLaunchingAction?.Invoke(application, launchOptions);
|
|
return true;
|
}
|
|
/// <summary>
|
/// 注册远程消息推送
|
/// </summary>
|
/// <param name="application">Application.</param>
|
void registerForRemoteNotification(UIApplication application)
|
{
|
if (8 <= int.Parse(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]))
|
{
|
// 1.注册UserNotification,以获取推送通知的权限
|
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, null);
|
application.RegisterUserNotificationSettings(settings);
|
|
// 2.注册远程推送
|
application.RegisterForRemoteNotifications();
|
}
|
else
|
{
|
application.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.NewsstandContentAvailability | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);
|
}
|
}
|
public override void OnResignActivation(UIApplication application) => OnResignActivationAction?.Invoke(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) => DidEnterBackgroundAction?.Invoke(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.
|
|
public override void WillEnterForeground(UIApplication application) => WillEnterForegroundAction?.Invoke(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)
|
{
|
AVAudioSession.SharedInstance().SetActive(true);
|
OnActivatedAction?.Invoke(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.
|
}
|
|
public override void WillTerminate(UIApplication application) => WillTerminateAction?.Invoke(application);// Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
|
/// <summary>
|
/// 注册远程消息推送回调方法
|
/// </summary>
|
/// <param name="application">Application.</param>
|
/// <param name="deviceToken">Device token.</param>
|
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
|
{
|
var bytes = new byte[deviceToken.Length];
|
System.Runtime.InteropServices.Marshal.Copy(deviceToken.Bytes, bytes, 0, bytes.Length);
|
|
var stringBuilder = new System.Text.StringBuilder();
|
for (int i = 0; i < bytes.Length; i++)
|
{
|
stringBuilder.AppendFormat("{0:x2}", bytes[i]);
|
}
|
var newDeviceToken = stringBuilder.ToString();
|
|
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
|
|
if (oldDeviceToken != newDeviceToken)
|
{
|
// Save new device token
|
NSUserDefaults.StandardUserDefaults.SetString(newDeviceToken, "PushDeviceToken");
|
}
|
DeviceTokenAction?.Invoke(newDeviceToken);
|
}
|
|
public virtual void setHDLRootViewController() {
|
|
|
}
|
|
}
|
}
|