我在Xamarin.iOS应用程序中开发了FCM通知。现在我需要通知点击导航,我可以处理它去特定的屏幕/ viewController of app
我的appDelegate类包含所有重载方法
namespace edTheS
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate,IUNUserNotificationCenterDelegate, IMessagingDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
LoginViewController loginCntl = new LoginViewController();
var loginCntl1 = new LoginViewController();
this.navCntl = new UINavigationController(loginCntl1);
this.navCntl.NavigationBarHidden = true;
Window.MakeKeyAndVisible();
InstanceId.Notifications.ObserveTokenRefresh(TokenRefreshNotification);
//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();
App.Configure();
return true;
}
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 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 WillTerminate(UIApplication application)
{
// Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
}
// You'll need this method if you set "FirebaseAppDelegateProxyEnabled": NO in GoogleService-Info.plist
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
SISConst.PushInfo = new PushNotification();
SISConst.PushInfo.DeviceID = deviceToken.ToString();
NSUserDefaults.StandardUserDefaults.SetString(deviceToken.ToString(),"dToken");
InstanceId.SharedInstance.SetApnsToken (deviceToken, Firebase.InstanceID.ApnsTokenType.Sandbox);
//InstanceId.SharedInstance.a
#if DEBUG
Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Sandbox);
#endif
#if RELEASE
Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Prod);
#endif
}
//public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
//{
// Console.WriteLine("Failed to register for remote notifications {0}", error.LocalizedDescription);
//}
//FCM-- This method calling when notifications delivering
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
// Generate custom event
NSString[] keys = { new NSString("Event_type") };
NSObject[] values = { new NSString("Recieve_Notification") };
var parameters = NSDictionary<NSString, NSObject>.FromObjectsAndKeys(keys, values, keys.Length);
//Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
string studentId = string.Empty, OrgNameSubTtl = string.Empty, moduleTitle = string.Empty, bodyText = string.Empty;
// Send custom event
Firebase.Analytics.Analytics.LogEvent("CustomEvent", parameters);
if (application.ApplicationState == UIApplicationState.Active)
{
//var title = notification.Request.Content.Title;
var remotePushData = userInfo.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString());
ScheduleNotification(moduleTitle.ToString(),OrgNameSubTtl.ToString(),bodyText.ToString());
}
else if(application.ApplicationState == UIApplicationState.Background)
{
var remotePushData = userInfo.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString());
ScheduleNotification(moduleTitle.ToString(), OrgNameSubTtl.ToString(), bodyText.ToString());
}
else if(application.ApplicationState == UIApplicationState.Inactive)
{
var remotePushData = userInfo.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString());
ScheduleNotification(moduleTitle.ToString(), OrgNameSubTtl.ToString(), bodyText.ToString());
}
}
//FCM
// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
string studentId = string.Empty, OrgNameSubTtl = string.Empty,moduleTitle=string.Empty,bodyText=string.Empty;
//var title = notification.Request.Content.Title;
var remotePushData = notification.Request.Content.UserInfo.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString());
ScheduleNotification(moduleTitle,OrgNameSubTtl,bodyText);
}
//Workaround for handling notifications in background for iOS 10
[Export ("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
Console.WriteLine("Notifications clicked......");
}
public override void DidEnterBackground(UIApplication application)
{
//FCM
Messaging.SharedInstance.ShouldEstablishDirectChannel = false;
Console.WriteLine("Disconnected from FCM");
}
void ScheduleNotification(string title,string OrgSubtitle,string body)
{
//Create content
var content = new UNMutableNotificationContent();
content.Title = title;
content.Subtitle = OrgSubtitle;
content.Body = body;
content.Badge = 0;
content.CategoryIdentifier = "categoryID";
content.Sound = UNNotificationSound.Default;
// Fire trigger in one seconds
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
var requestID = "customNotification";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
// This is the line that does the trick
UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
if (err != null)
{
// Report error
System.Console.WriteLine("Error: {0}", err);
}
else
{
// Report Success
System.Console.WriteLine("Notification Scheduled: {0}", request);
}
});
}
}
}
从上面的代码中没有一个方法调用通知点击。 请建议我更好的方式
CustomUNUserNotificationCenterDelegate是单独的类
public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
string studentId = string.Empty, OrgNameSubTtl = string.Empty,moduleNmae=string.Empty,bodyText=string.Empty;
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
// Here you handle the user taps
completionHandler();
var remotePushData = response.Notification.Request.Content.UserInfo.ToDictionary(i => i.Key.ToString(), i => i.Value.ToString());
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
}