wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using AVFoundation;
using com.hdl.on;
using CoreLocation;
using Foundation;
 
using UIKit;
 
namespace Shared
{
    public class BaseApplicationDelegateForHdlOn : 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 ();
            locate ();
            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);
            }
        }
 
        #region GPS 定位
        // 判断定位操作是否被允许  
        CLLocationManager locationManager;
        void locate ()
        {
            //if (CLLocationManager.LocationServicesEnabled)
            {
                //定位初始化  
                locationManager = new CLLocationManager ();
                //设置定位精度
                locationManager.DesiredAccuracy = 100;
                //设置是否允许系统自动暂停定位
                locationManager.PausesLocationUpdatesAutomatically = false;
                //设置定位更新的最小距离
                locationManager.DistanceFilter = 10;
 
                locationManager.LocationsUpdated += (sender, e) => {
                    var location = e.Locations [e.Locations.Length - 1];
                    Shared.Application.LocationAction?.Invoke (location.Coordinate.Longitude, location.Coordinate.Latitude);
                };
                if (8 <= int.Parse (UIDevice.CurrentDevice.SystemVersion.Split ('.') [0])) {
                    //定位管理者需要一直请求定位服务
                    locationManager.RequestWhenInUseAuthorization ();
                }
                //if (9 <= int.Parse(UIDevice.CurrentDevice.SystemVersion.Split('.')[0]))
                //{
                //    //需要允许后台更新数据更新
                //    //locationManager.AllowsBackgroundLocationUpdates = true;
                //    locationManager.RequestAlwaysAuthorization();
                //}
 
                locationManager.StartUpdatingLocation ();//开启定位  
            }
        }
 
        #endregion
        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 deviceTokenDescription = deviceToken.Description;
            if (!string.IsNullOrWhiteSpace (deviceTokenDescription)) {
                deviceTokenDescription = deviceTokenDescription.Trim ('<').Trim ('>').Replace (" ", "");
                Shared.HDLUtils.WriteLine ($"DeviceToken:{deviceTokenDescription}");
            }
            // Get previous device token
            var oldDeviceTokenDescription = NSUserDefaults.StandardUserDefaults.StringForKey ("PushDeviceToken");
 
            // Has the token changed?
            if (oldDeviceTokenDescription != deviceTokenDescription) {
                // Save new device token
                NSUserDefaults.StandardUserDefaults.SetString (deviceTokenDescription, "PushDeviceToken");
            }
            DeviceTokenAction?.Invoke (deviceTokenDescription);
        }
    }
}