Created
April 15, 2017 18:05
-
-
Save anonymous/edbb57bdd78b9a2fbcc12571f2af901e 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Foundation; | |
using UIKit; | |
using Firebase.Analytics; | |
using UserNotifications; | |
using Firebase.CloudMessaging; | |
using Firebase.InstanceID; | |
using Plugin.Share; | |
using System.Threading.Tasks; | |
namespace TopEnTwelForms.iOS | |
{ | |
[Register ("AppDelegate")] | |
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate | |
{ | |
public override bool FinishedLaunching (UIApplication app, NSDictionary options) | |
{ | |
global::Xamarin.Forms.Forms.Init (); | |
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false); | |
UIApplication.SharedApplication.SetStatusBarHidden(false, false); | |
LoadApplication(new App()); | |
Firebase.Analytics.App.Configure(); | |
// 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 = this; | |
// 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(); | |
Messaging.SharedInstance.Connect(error => { | |
if (error != null) | |
{ | |
// Handle if something went wrong while connecting | |
Console.WriteLine("Something went wrong: " + error); | |
UIAlertView alert = new UIAlertView() | |
{ | |
Title = "Error", | |
Message = error.ToString() | |
}; | |
alert.AddButton("OK"); | |
alert.Show(); | |
} | |
else | |
{ | |
// Let the user know that connection was successful | |
Console.WriteLine("Connection was successful"); | |
} | |
}); | |
InstanceId.Notifications.ObserveTokenRefresh((sender, e) => { | |
// Note that this callback will be fired everytime a new token is generated, including the first | |
// time. So if you need to retrieve the token as soon as it is available this is where that | |
// should be done. | |
var refreshedToken = InstanceId.SharedInstance.Token; | |
// Do your magic to refresh the token where is needed | |
}); | |
return base.FinishedLaunching (app, options); | |
} | |
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 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 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 | |
System.Console.WriteLine(userInfo); | |
} | |
// To receive notifications in foreground on iOS 10 devices. | |
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")] | |
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) | |
{ | |
// Do your magic to handle the notification data | |
System.Console.WriteLine(notification.Request.Content.UserInfo); | |
} | |
// Receive data message on iOS 10 devices. | |
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage) | |
{ | |
Console.WriteLine(remoteMessage.AppData); | |
} | |
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")] | |
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) | |
{ | |
// Do your magic to handle the notification data | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment