从未在Xamarin.iOS

时间:2018-02-26 15:14:09

标签: ios firebase push-notification xamarin.ios firebase-cloud-messaging

我尝试使用Xamarin和Xamarin.Firebase.iOS.CloudMessaging包为我的iOS应用程序实现推送通知。

我已经完成了所有设置和工作,例如:当应用程序位于前台时,我可以接收通知(包含并且不包含" content-available"标记)并与它们进行交互(点击通知等)。

但是,当应用程序在后台时,我会收到通知但是,不会调用任何回调。根据我的理解,{:1}}应在以下时间调用:

  1. 无论"didReceiveRemoteNotification"标记是否已启用
  2. ,应用程序都在前台
  3. 如果应用程序在后台或已暂停,则启用"content-available"标记
  4. 当用户点按通知时
  5. 以下是我在AppDelegate.cs中实现的功能:

    "content-available"

    我已经在iOS 10.3.3和iOS 11.2上试过了这些。以下是两个版本调用的函数:

    1. 当应用程序在前台时
      • 在iOS 10.3.3上:public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); Firebase.Core.App.Configure(); FireBaseRegistration(); return base.FinishedLaunching(app, options); } private void FireBaseRegistration() { // 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; } else { // iOS 9 or before var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null); UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); } //Register for APNs notifications UIApplication.SharedApplication.RegisterForRemoteNotifications(); Messaging.SharedInstance.Delegate = this; var token = InstanceId.SharedInstance.Token; Debug.WriteLine(token); //////Connect to FCM (Only used for Foreground notifications) Messaging.SharedInstance.ShouldEstablishDirectChannel = true; // Monitor token generation 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. token = InstanceId.SharedInstance.Token; Console.WriteLine(token); }); } //Register APNs token because method swizzling is de-activated public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { Console.WriteLine("Registered for remote notifications"); Console.WriteLine(deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None)); Console.WriteLine(deviceToken); } [Export("messaging:didReceiveMessage:")] public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage) { // Do your magic to handle the notification data Console.WriteLine("iOS 11 Foreground"); } //Shows local notification and is called when user taps notification [Export("userNotificationCenter:DidReceiveRemoteNotification:withCompletionHandler:")] public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { Console.WriteLine("Received a notficaiton"); completionHandler(UIBackgroundFetchResult.NewData); } //To receive notifications in foreground on iOS 11 devices. [Export("userNotificationCenter:willPresent:withCompletionHandler:")] public void WillPresent(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) { Console.WriteLine("Handling iOS 11 foreground notification"); completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert); } ////Called when tapping notification [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")] public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) { Console.WriteLine("Handling push notificaiton interaction"); completionHandler(); } //Receive data message on iOS 10 devices. public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage) { Console.WriteLine("Handling iOS 10 data message notification"); } //// To receive notifications in foreground on iOS 10 devices. [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")] public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) { Console.WriteLine("Handling foreground notification"); completionHandler(UNNotificationPresentationOptions.Alert); } (包含和不包含WillPresentNotification()标记)
      • 在iOS 11.2上:"content-available"(包含和不包含WillPresentNotification()标记)
    2. 当应用程序在后台时:
      • 在iOS 10.3.3上:没有什么可以成为"content-available"
      • 在iOS 11.2上:没有什么事情可以DidReceiveRemoteNotification
    3. 我在Info.plist文件中激活了远程通知和后台获取后台模式。

      包裹信息:

      • Xamarin.forms:2.5.0.280555
      • Xamarin.Firebase.iOS.CloudMessage:2.0.4.1
      • Xamarin.Firebase.iOS.Core:4.0.13
      • Xamarin.Firebase.iOS.InsstanceID:2.0.8

      说明:

      DidReceiveRemoteNotification函数在Xamarin示例中实现,但在我的情况下,永远不会被调用。

      当用户点击通知时,会调用DidReceiveMessage

      DidReceiveNotificationResponse也永远不会被调用。

      我已尝试通过firebase API直接通过APN(使用推送器应用程序)发送通知。

0 个答案:

没有答案