【问题标题】:Xamarin Form(PCL) IOS Firebase Push Message not workingXamarin Form(PCL) IOS Firebase 推送消息不起作用
【发布时间】:2017-05-04 07:29:04
【问题描述】:

我创建了一个 xamarin 表单项目,并在 Android 和 IOS 项目中集成了 firebase 推送通知。它在 Android 上运行良好,但不适用于 iOS。 我在 iOS 项目中下载并添加了 GoogleService-info.plist,将其 Build Action 设置为 BundleResource。

AppDelegates.cs

    namespace PushNotification.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            RegisterForNotificationFCM();

            return base.FinishedLaunching(app, options);
        }


        private void RegisterForNotificationFCM()
        {

            //Firebase Cloud Messaging Configuration

            //Get permission for notification
            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // iOS 10
                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;

                Messaging.SharedInstance.RemoteMessageDelegate = this as IMessagingDelegate;

            }
            else
            {
                // iOS 9 <=
                var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
                var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            }

            UIApplication.SharedApplication.RegisterForRemoteNotifications();

            Firebase.Analytics.App.Configure();

            Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                var newToken = Firebase.InstanceID.InstanceId.SharedInstance.Token;
                System.Diagnostics.Debug.WriteLine(newToken);

                connectFCM();
            });

        }


        public override void DidEnterBackground(UIApplication uiApplication)
        {
            Messaging.SharedInstance.Disconnect();
        }

        public override void OnActivated(UIApplication uiApplication)
        {
            connectFCM();
            base.OnActivated(uiApplication);
        }

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Prod);
        }

        //Fire when background received notification is clicked
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
            //Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
            System.Diagnostics.Debug.WriteLine(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);

            // Send custom event
            Firebase.Analytics.Analytics.LogEvent("CustomEvent", parameters);

            if (application.ApplicationState == UIApplicationState.Active)
            {
                System.Diagnostics.Debug.WriteLine(userInfo);
                var aps_d = userInfo["aps"] as NSDictionary;
                var alert_d = aps_d["alert"] as NSDictionary;
                var body = alert_d["body"] as NSString;
                var title = alert_d["title"] as NSString;
                debugAlert(title, body);
            }
        }

        private void connectFCM()
        {
            Messaging.SharedInstance.Connect((error) =>
            {
                if (error == null)
                {
                    Messaging.SharedInstance.Subscribe("/topics/topicName");
                }
                System.Diagnostics.Debug.WriteLine(error != null ? "error occured" : "connect success");
            });
        }

        private void debugAlert(string title, string message)
        {
            var alert = new UIAlertView(title ?? "Title", message ?? "Message", null, "Cancel", "OK");
            alert.Show();
        }


    }
}

在 IOS 项目中添加了所有必需的 Firebase 库,并且构建良好。但是IOS模拟器没有收到通知。告诉我我错过了什么。

【问题讨论】:

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


    【解决方案1】:

    已解决

    我遇到了同样的问题,并且处理了一两天。问题归结为正在使用的选定配置文件。当我在苹果开发门户中更改我的应用程序 ID 以使用推送通知并下载我的配置文件时,它创建了第二个下载的同名配置文件。当它试图选择正确的一个时,这会导致错误。删除了旧的配置文件,现在一切都很好。

    【讨论】:

      【解决方案2】:

      您无法在模拟器中测试推送通知

      请查看Prerequisites

      For Cloud Messaging:
      
       - A physical iOS device 
       - APNs certificate with Push Notifications enabled
       - In Xcode, enable Push Notifications in App > Capabilities
      

      【讨论】:

      • 好的,但是我上面提到的这些都可以吗?
      • 我认为您应该在实际的物理设备上对其进行测试以确认预期结果
      • @Burhan Firebase Push Messages 是 Xamarin 的(绑定)库。如果原始库(用于本机 iOS)不能在模拟器上运行消息,那么 Xamarin 库也不会。我认为没有任何理由将其添加到文档中,除非功能不同
      • 我明白你的意思,但我在设备上查看它仍然没有推送?
      • 我错过了什么?
      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      相关资源
      最近更新 更多