Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Register("AppDelegate")]
- public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
- {
- public override bool FinishedLaunching(UIApplication app, NSDictionary options)
- {
- global::Xamarin.Forms.Forms.Init();
- LoadApplication(new App("",""));
- RequestPushPermissionsAsync();
- _launchoptions = options;
- return base.FinishedLaunching(app, options);
- }
- NSDictionary _launchoptions;
- public override void OnActivated(UIApplication uiApplication)
- {
- base.OnActivated(uiApplication);
- if (_launchoptions != null && _launchoptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
- {
- var notfication = _launchoptions[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
- PresentNotification(notfication);
- }
- _launchoptions = null;
- }
- async Task RequestPushPermissionsAsync()
- {
- var requestResult = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(
- UNAuthorizationOptions.Alert
- | UNAuthorizationOptions.Badge
- | UNAuthorizationOptions.Sound);
- bool approved = requestResult.Item1;
- NSError error = requestResult.Item2;
- if (error == null)
- {
- if (!approved)
- {
- Console.Write("Permission to receive notification was not granted");
- return;
- }
- var currentSettings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync();
- if (currentSettings.AuthorizationStatus != UNAuthorizationStatus.Authorized)
- {
- Console.WriteLine("Permissions were requested in the past but have been revoked (-Settings app)");
- return;
- }
- UIApplication.SharedApplication.RegisterForRemoteNotifications();
- }
- else
- {
- Console.Write($"Error requesting permissions: {error}.");
- }
- }
- public async override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
- {
- if (deviceToken == null)
- {
- return;
- }
- Console.WriteLine($"Token received: {deviceToken}");
- await SendRegistrationTokenToMainPRoject(deviceToken);
- }
- async Task SendRegistrationTokenToMainPRoject(NSData deviceToken)
- {
- MessagingCenter.Send<object, string>(this, "fcmtoken", deviceToken.ToString());
- }
- public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
- {
- Console.WriteLine($"Failed to register for remote notifications: {error.Description}");
- }
- public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
- Action<UIBackgroundFetchResult> completionHandler)
- {
- PresentNotification(userInfo);
- completionHandler(UIBackgroundFetchResult.NoData);
- try
- {
- UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
- NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
- var message = (aps[new NSString("webContentList")] as NSString).ToString();
- LoadApplication(new App("", message));
- }
- catch (Exception ex)
- {
- //LogInfo.ReportErrorInfo(ex.Message, ex.StackTrace, "AppDelegate-DidReceiveRemoteNotification");
- }
- }
- void PresentNotification(NSDictionary userInfo)
- {
- NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
- var msg = string.Empty;
- if (aps.ContainsKey(new NSString("alert")))
- {
- msg = (aps[new NSString("alert")] as NSString).ToString();
- }
- if (string.IsNullOrEmpty(msg))
- {
- msg = "(unable to parse)";
- }
- MessagingCenter.Send<object, string>(this, App.NotificationReceivedKey, msg);
- }
- }
- {
- "multicast_id": 8754155136812875313,
- "success": 0,
- "failure": 1,
- "canonical_ids": 0,
- "results": [
- {
- "error": "InvalidRegistration"
- }
- ]
- }
- {
- "to" : "d20ad003 7473bfba 85dffc33 1534decf b4b886f1 c738878f fd7f2c60 d9dabc36",
- "collapse_key" : "type_a",
- "data" : {
- "body" : "Body of Your Notification in Data",
- "title": "Title of Your Notification in Title",
- "key_1" : "Value for key_1",
- "key_2" : "Value for key_2"
- },
- "notification" : {
- "body" : "Body of Your Notification",
- "title": "Title of Your Notification",
- "sound": "default",
- "content_available" : true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement