【问题标题】:Firebase APN notification are not in the tray when app is in the background当应用程序在后台时,Firebase APN 通知不在托盘中
【发布时间】:2017-05-05 08:27:02
【问题描述】:

由于某些原因,当应用在后台时,通过 Firebase 发送的通知不会进入托盘。这是初始化 Firebase 的代码(我们目前正在使用 VS 2017 中的 Xamarin 在 iOS 10 上进行测试)。在 AppDelegate.cs 中:

    public void InitFirebase()
    {
        // Configure Firebase
        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) =>
            {
                Log.Info("BoaTan", "RequestAuthorization: {0}", granted);
            });

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = this;

            // For iOS 10 data message (sent via FCM)
            Firebase.CloudMessaging.Messaging.SharedInstance.RemoteMessageDelegate = this;
            // Monitor token generation
            InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                Log.Info("BoaTan", "New firebase token received {0}", PlatformEntrance.Token);

                LoginViewModel viewModel = LoginView.Me.ViewModel as LoginViewModel;

                viewModel.UpdateFirebaseToken(PlatformEntrance.Token);
            });

            Firebase.CloudMessaging.Messaging.SharedInstance.Connect(error =>
            {
                if (error != null)
                {
                    Log.Error("BoaTan", error.DebugDescription);
                }
                else
                {
                    Log.Info("BoaTan", "Connection to Firebase messaging succeeded");
                }
            });

            // Monitor token generation
            InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                SendTokenToServer();
            });
        }
        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();
    }

在 AppDelegate.cs 中我们也有以下代码来接收消息:

        public override void WillEnterForeground(UIApplication application)
    {
        Firebase.CloudMessaging.Messaging.SharedInstance.Connect((NSError error) =>
        {
            Log.Info("BoaTan", "WillEnterForeground: Connected to Firebase messaging ({0})", error?.Description);
        });

        base.WillEnterForeground(application);
    }

    public override void DidEnterBackground(UIApplication application)
    {
        Firebase.CloudMessaging.Messaging.SharedInstance.Disconnect();

        Log.Info("BoaTan", "DidEnterBackground: Disconnected from Firebase messaging");

        base.DidEnterBackground(application);
    }

    // To receive notifications in foregroung on iOS 9 and below.
    // To receive notifications in background in any iOS version
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        Log.Info("BoaTan", "DidReceiveRemoteNotification: Disconnected from Firebase messaging");

        SendDataMessage(userInfo);
    }

    // To receive notifications in foreground on iOS 10 devices.
    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        Log.Info("BoaTan", "WillPresentNotification: Disconnected from Firebase messaging");

        SendDataMessage(notification.Request.Content.UserInfo);
    }

    public void ApplicationReceivedRemoteMessage(RemoteMessage message)
    {
        SendDataMessage(message.AppData);
    }

    /// <summary>
    /// Use MvvmCross messaging to send a message to subcribers.
    /// </summary>
    /// <param name="dictionary"></param>
    private void SendDataMessage(NSDictionary dictionary)
    {
        LogReceivedInfo(dictionary);

        NSObject data;
        NSString key = new NSString("data");

        if (dictionary.TryGetValue(key, out data))
        {
            Log.Info("BoaTan", "Data: {0}", data);

            Settings.Notification = JsonConvert.DeserializeObject<LoginNotificationParameter>((NSString)data);

            ServicesHelper.SendMessage(this);
        }
    }

    private void LogReceivedInfo(NSDictionary keyvalues)
    {
        Log.Info("BoaTan", "-----------------------------------------------------------");

        foreach (var keyval in keyvalues)
        {
            Log.Info("BoaTan", "Key: {0} Value: {1}", keyval.Key, keyval.Value);
        }

        Log.Info("BoaTan", "-----------------------------------------------------------");
    }
}

当应用程序在前台时,消息完美到达。所有消息都会排队,直到应用再次进入前台。

这在 info.plist 中:

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
    <string>fetch</string>
</array>

当我进入 Firebase 控制台并在那里以多种变体编写消息时,消息也不会到达托盘中,这使我得出以下结论:

  1. 应用缺少一些配置,告诉 iOS 我正在等待消息。
  2. Apple 开发者控制台中缺少 APN 配置。
  3. Firebase/iOS 配置/初始化中缺少某些内容。

排列是无穷无尽的。谁有答案?然后还有 iOS 9 的挑战。

【问题讨论】:

  • 没人想吗?

标签: firebase apple-push-notifications ios10 firebase-cloud-messaging


【解决方案1】:

我没有在 iOS 上使用 Firebase 的经验,但在 Android 上我遇到了同样的问题。 Firebase 有两种类型的消息:Notification messageData message,请参阅 About FCM Messages 在 Android 上,Notification message 仅在应用程序处于前台时可见。也许这也是你的问题

【讨论】:

  • 在 Android 上,我们的东西运行良好。在 FCM 消息文档中,它说当应用程序在后台时,通知消息被传递到托盘中。没想到会这样。
【解决方案2】:

不知道它为什么开始工作,但现在可以工作了。从我的 iPad 删除应用程序并重新安装后,托盘也开始工作。我的猜测是重新部署会保持原样设置,并且在重新安装后这些设置正确。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多