错误:无法完成操作.(com.google.fcm 错误 501)



我正在将我的 Xamarin.iOS 应用与 Firebase 推送通知集成,但收到此错误 错误:无法完成操作。(com.google.fcm 错误 501(我添加了开发证书 APNS 密钥并满足其他要求,但无法获取 FCM 令牌。有人可以帮忙吗?提前谢谢你。

using System;
using Foundation;
using UIKit;
using App.Pages;
using SupportWidgetXF.iOS;
using UserNotifications;
using Firebase.CloudMessaging;
using Firebase.InstanceID;

namespace App.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, 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)
{
UIApplication.SharedApplication.RegisterForRemoteNotifications();
Rg.Plugins.Popup.Popup.Init();
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
global::Xamarin.Forms.Forms.Init();
// Firebase.Core.App.Configure();
// get permission for notification
//  SupportWidgetXFSetup.Initialize(this);
LoadApplication(new App());
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;
Messaging.SharedInstance.Delegate = this;
var toke = Messaging.SharedInstance.FcmToken;
var tokenx = Messaging.SharedInstance.ApnsToken;
string token = InstanceId.SharedInstance.Token;
//  var token = Messaging.SharedInstance.FcmToken;
// For iOS 10 data message (sent via FCM)
}
else
{
// iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
InstanceId.Notifications.ObserveTokenRefresh((sender, e) => {
var newToken = InstanceId.SharedInstance.Token;
// if you want to send notification per user, use this token
System.Diagnostics.Debug.WriteLine(newToken);
connectFCM();
});            
string fcm = Messaging.SharedInstance.FcmToken;
//    Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
// PushNotificationManager.Initialize(options, true);
return base.FinishedLaunching(app, options);
}
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
Console.WriteLine($"Firebase registration token: {fcmToken}");
// Dependency.DataManager.FCMToken = fcmToken;
}
[Export("messaging:didRefreshRegistrationToken:")]
public void DidRefreshRegistrationToken(Messaging messaging, string fcmToken)
{
System.Diagnostics.Debug.WriteLine( $" FCM Token: { fcmToken } ");
}
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)
{
#if DEBUG
//InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Sandbox);
#endif
#if RELEASE
Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Prod);
#endif
}
// iOS 9 <=, fire when recieve notification foreground
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);
// 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);
}
}
// iOS 10, fire when recieve notification foreground
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
var title = notification.Request.Content.Title;
var body = notification.Request.Content.Body;
debugAlert(title, body);
}
private void connectFCM()
{
Firebase.Core.App.Configure();
// Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
Messaging.SharedInstance.Connect((error) =>
{
if (error == null)
{
Messaging.SharedInstance.Subscribe("/topics/all");
}
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();
}
}
}

问题是我正在模拟器上测试,但是当我在物理设备上进行测试时。 它有效。但是,我还将我的配置文件切换到另一个配置文件而不是将其设置回来,这可能有助于解决该问题,现在它也在模拟器上工作。

相关内容

  • 没有找到相关文章

最新更新