gpt4 book ai didi

ios - 适用于 iOS 的 Xamarin Firebase 出现 fcm 501 错误

转载 作者:行者123 更新时间:2023-11-29 00:06:53 25 4
gpt4 key购买 nike

我已经为这个问题苦苦挣扎了好几天,并且发现了很多关于这个主题的其他帖子,但似乎没有什么可以解决我遇到的问题。我觉得这可能很简单,因为我已经多次尝试减少项目并总是遇到同样的失败。我可以在 Android 中很好地接收 Firebase 消息,但 iOS 总是让我失望。

我正在使用装有 iOS 10 的手机进行测试。调试代码总是会返回一个 token ,但在连接时会出现错误“操作无法完成。 (com.google.fcm 错误 501.)' 被返回。我附上了一些示例代码 - 这令人沮丧

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
//
// 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 FirebaseTest.App (""));

RegisterForNotificationFCM();
return base.FinishedLaunching (app, options);
}
void RegisterForNotificationFCM()
{
// 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.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);
}

UIApplication.SharedApplication.RegisterForRemoteNotifications();

Firebase.Analytics.Loader loader1 = new Firebase.Analytics.Loader();
Firebase.InstanceID.Loader loader2 = new Firebase.InstanceID.Loader();

//Firebase.Core.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.ShouldEstablishDirectChannel = false;
}

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

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

public void DidRefreshRegistrationToken(Messaging msg, string str)
{
}

//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);
}
}

[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);
}

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

当您变得越来越绝望时,这些功能已从同一主题的许多其他帖子中删除。我认为 firebase 控制台已经设置好了。我已将 iOS 作为一个应用程序添加到正在运行的 Android 应用程序旁边,并添加了在 Mac 上创建的身份验证 key 。 (令人惊讶的是,即使没有这个 iOS 应用程序作为 firebase 项目的一部分,我仍然会得到一个 token - 这不是我所期望的)已经为此应用程序创建了一个证书,并且已经重新创建了配置文件并删除了旧的。

我不确定还有什么要补充的——请有人把我从痛苦中解救出来......

最佳答案

终于到了。供其他人检查的内容 - 将 App.Configure() 移至 connectFCM

    private void connectFCM()
{
Firebase.Core.App.Configure();

Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
}

并将以下内容添加到 info.plist

<key>NSExceptionDomains</key>
<dict>
<key>firebaseio.com</key>
<dict>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>

关于ios - 适用于 iOS 的 Xamarin Firebase 出现 fcm 501 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com