gpt4 book ai didi

c# - Xamarin.iOS Firebase 未接收推送通知但接收 token

转载 作者:行者123 更新时间:2023-11-29 05:22:59 28 4
gpt4 key购买 nike

我正在 Xamarin.Forms 项目中设置推送通知。我已经为 Xamarin.Forms.Android 完成了所有操作,它工作没有任何问题,但我在 iOS 部分遇到了很多麻烦。这不是我第一次这样做,但我仍然不明白发生了什么。

我做了什么:
1.包含Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2(这不是最新的,但由于库错误,最新的甚至没有构建)
2. 创建 Firebase 应用程序并遵循常规设置,上传我的 .p12 用于推送通知并添加我的团队 ID。
p12
3.添加我的GoogleService-Info.plist并将其BuildAction设置为“BundleResource”
4.更新了我的Info.plist
infoplist
5. 确保我在 Apple Developer Program 中的 App ID 包含推送通知和正确的证书
cert
6. 添加了与 UserNotifications.IUNUserNotificationCenterDelegate、IMessagingDelegate 接口(interface)相关的每一段代码
7. 从

检索我的 token
Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
var token = Messaging.SharedInstance.FcmToken;

if (!string.IsNullOrEmpty(token))
{

//AppData.Instance.TokenMobile = newToken;
}
});
  • 返回 Firebase 控制台,创建测试推送并尝试通过“Inviamessagio di prova”(“发送消息”)发送它,这让我指定了一个 token 并通过发布发送。
    not1
    not2
  • 我的AppDelegate.cs

        public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, UserNotifications.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();
    global::Xamarin.Forms.FormsMaterial.Init();
    LoadApplication(new App());

    Firebase.Core.App.Configure();
    // For iOS 10 data message (sent via FCM)
    Messaging.SharedInstance.Delegate = this;

    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.Current.Delegate = this;

    // iOS 10 or later
    var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
    UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
    {
    if (granted)
    {
    InvokeOnMainThread(() =>
    {
    UIApplication.SharedApplication.RegisterForRemoteNotifications();
    });
    }
    });
    }
    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();

    // Handle token as you wish
    Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
    {
    var token = Messaging.SharedInstance.FcmToken;

    if (!string.IsNullOrEmpty(token))
    {

    //AppData.Instance.TokenMobile = newToken;
    }
    });


    UIApplication.SharedApplication.StatusBarHidden = true;
    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

    return base.FinishedLaunching(app, options);
    }

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    {
    }

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
    }

    [Export("messaging:didRefreshRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
    {
    Console.WriteLine($"Firebase registration token: {fcmToken}");

    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
    //Tested
    //Messaging.SharedInstance.ApnsToken = deviceToken;
    }

    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
    var userInfo = notification.Request.Content.UserInfo;

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

    // Print full message.
    Console.WriteLine(userInfo);

    // Change this to your preferred presentation option
    completionHandler(UNNotificationPresentationOptions.None);
    }

    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action boh)
    {

    }

    [Export("messaging:didReceiveMessage:")]
    public void DidReceiveMessage(Messaging messaging, RemoteMessage message)
    {

    }
    }

    我还将此 key 添加到 Entitlment.plist 文件中:

    <dict>
    <key>aps-environment</key>
    <string>development</string>
    </dict>

    此时,我希望在我的应用程序中收到一些东西,但我什么也收不到。
    我在尝试实现的每个回调中放置了一些断点,但没有一个被调用。
    如果它有帮助,甚至不会调用“DidReceiveRegistrationToken”方法。

    最佳答案

    解决了问题。
    我不知 Prop 体原因,但我在 GoogleService-Info.plist 中发现了与 Firebase 控制台中指定的 API key 不同的 API key 。我更新了 key ,现在正在收到通知。

    关于c# - Xamarin.iOS Firebase 未接收推送通知但接收 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58482697/

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