C# Xamarin forms CFB通知:如何在一次注册设备后刷新令牌?

C# Xamarin forms CFB通知:如何在一次注册设备后刷新令牌?,c#,firebase,xamarin,push-notification,C#,Firebase,Xamarin,Push Notification,我使用Xamarin.forms.iOS创建了应用程序,并使用Plugin.FirebasePushNotification通过firebase发送通知 一旦我注册了设备,获取令牌就没有问题了,但是,在我删除应用并再次测试之后,应用没有获取令牌。 我再也不能收到通知了 AppDelegate.cs [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.Form

我使用Xamarin.forms.iOS创建了应用程序,并使用Plugin.FirebasePushNotification通过firebase发送通知

一旦我注册了设备,获取令牌就没有问题了,但是,在我删除应用并再次测试之后,应用没有获取令牌。 我再也不能收到通知了

AppDelegate.cs

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    NSDictionary _launchOptions;
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        ImageCircleRenderer.Init();
        LoadApplication(new App());
        AnimationViewRenderer.Init();

        _launchOptions = options;

        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        SQLitePCL.Batteries.Init();

        ZXing.Net.Mobile.Forms.iOS.Platform.Init();
        FirebasePushNotificationManager.Initialize(options, true);

        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Sound,
                                                                    (granted, error) =>
                                                                    {
                                                                        if (granted)
                                                                            InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
                                                                    });
        }
        else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                    new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();
        }
        else
        {
            UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
            UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
        }


        CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
        {
            System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
            UIPasteboard clipboard = UIPasteboard.General;
            clipboard.String = p.Token;
            Helper.Settings.Token = p.Token;

        };

        CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
        {
            Dictionary<string, object> dic = p.Data as Dictionary<string, object>;
            if (dic["targetUserId"].ToString() != null && Helper.Settings.UserId == dic["targetUserId"].ToString())
            {
                System.Diagnostics.Debug.WriteLine("Received");
                Helper.Settings.userWhoSentNotiId = dic["userWhoSentNotiId"].ToString();
                Helper.Settings.callingStatus = true;                 FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;
                MessagingCenter.Send<object, string>(this, "CallingNotifications", dic["callingName"].ToString());
            }
        };

        CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
        {
            Dictionary<string, object> dic = p.Data as Dictionary<string, object>;
            Helper.Settings.userWhoSentNotiId = dic["userWhoSentNotiId"].ToString();
            Helper.Settings.callingStatus = true;
            MessagingCenter.Send<object, string>(this, "CallingNotifications", dic["callingName"].ToString());
        };
        return base.FinishedLaunching(app, options);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
    }

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        FirebasePushNotificationManager.DidReceiveMessage(userInfo);
        completionHandler(UIBackgroundFetchResult.NewData);
    }
}
[注册(“AppDelegate”)]
公共部分类AppDelegate:global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
NSDictionary _启动选项;
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
全局::Xamarin.Forms.Forms.Init();
ImageCirclerEnder.Init();
加载应用程序(新应用程序());
AnimationViewRenderer.Init();
_启动选项=选项;
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
SQLitePCL.batters.Init();
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
FirebasePushNotificationManager.Initialize(选项,true);
if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
UNUserNotificationCenter.Current.RequestAuthorization(未授权选项.Alert |未授权选项.Sound |未授权选项.Sound,
(已批准,错误)=>
{
如果(授予)
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemotonifications);
});
}
else if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
var pushSettings=UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
新NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(推送设置);
UIApplication.SharedApplication.RegisterForRemotonifications();
}
其他的
{
UIRemoteNotificationType notificationTypes=UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
CrossFirebasePushNotification.Current.OnTokenRefresh+=(s,p)=>
{
System.Diagnostics.Debug.WriteLine($“令牌:{p.TOKEN}”);
UIPasteboard剪贴板=UIPasteboard.General;
clipboard.String=p.Token;
Helper.Settings.Token=p.Token;
};
CrossFirebasePushNotification.Current.OnNotificationReceived+=(s,p)=>
{
字典dic=作为字典的p.数据;
如果(dic[“targetUserId”].ToString()!=null&&Helper.Settings.UserId==dic[“targetUserId”].ToString())
{
System.Diagnostics.Debug.WriteLine(“已接收”);
Helper.Settings.userWhoSentNotiId=dic[“userWhoSentNotiId”].ToString();
Helper.Settings.callingStatus=true;FirebasePushNotificationManager.CurrentNotificationPresentationOption=UNNotificationPresentationOptions.Alert;
MessagingCenter.Send(这是“CallingNotifications”,dic[“callingName”].ToString());
}
};
CrossFirebasePushNotification.Current.OnNotificationOpen+=(s,p)=>
{
字典dic=作为字典的p.数据;
Helper.Settings.userWhoSentNotiId=dic[“userWhoSentNotiId”].ToString();
Helper.Settings.callingStatus=true;
MessagingCenter.Send(这是“CallingNotifications”,dic[“callingName”].ToString());
};
返回基地。完成发射(应用程序,选项);
}
公共覆盖无效注册更正(UIApplication应用程序,NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemomentNotifications(deviceToken);
}
远程通知的公共覆盖无效注册失败(UIApplication应用程序,N错误)
{
FirebasePushNotificationManager.RemoteNotificationRegistration失败(错误);
}
public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
{
FirebasePushNotificationManager.DidReceiveMessage(用户信息);
completionHandler(UIBackgroundFetchResult.NewData);
}
}
我注意到应用程序没有启动 CrossFirebasePushNotification.Current.OnTokenRefresh


应用程序如何刷新令牌?

错误消息是什么?您是否尝试注册该设备?@WendyZang Helper.Settings.Token=p.Token中没有错误消息,只有p.Token;行是空的。我试着调试这行代码,但它没有被执行。我不知道为什么以及如何修复此问题。这是在您删除应用程序时发生的,您是否尝试在firebase中创建匹配的应用程序以再次测试?@WendyZang是!但它无法接收令牌。刷新令牌是如何工作的?我怎样才能解决这个问题?谢谢我尝试删除应用程序并重新创建它。也许是相似的。我无法毫无错误地获取令牌。您可以尝试在FCM中创建具有相同软件包名称的应用程序。删除项目的bin和obj文件夹,并添加新的json文件。请注意,您需要首先卸载手机中的应用程序。请确保您的设备已连接到internet。