Android(Xamarin)FCM推送通知OnRefreshToken未被调用,但令牌无效

时间:2018-10-30 09:10:34

标签: android xamarin push-notification xamarin.android firebase-cloud-messaging

我们有一个跨平台的应用程序(Xamarin)。对于推送通知,我们使用Plugin.PushNotification 1.3.0。 在iOS上,一切似乎都可以正常运行。但是在Android上有时会出现问题,我们的注册ID无效,或者至少看起来是这样。 当我们的服务器向FCM发送消息时,我们得到的响应是注册ID不再注册。这仅在某些设备上发生。到期时间没有意义,因为在一种情况下,到期时间是3天。

也没有OnTokenRefresh事件。打开应用程序时,我们总是将当前ID发送给服务器,当然,如果将触发OnTokenRefresh,我们也会将其发送给服务器。

有人有想法或至少知道问题所在吗? 如果由于某种原因关闭了应用程序且ID无效,会发生什么情况? 还是可能与Android Oreo有关系?我们正在尝试验证,但目前看来,它仅发生在最近的设备上。

我们已将问题缩小了一点。 在Android中,该事件被调用,但在Xamarin Forms中不被调用。如此有效:

        public override void OnCreate()
        {
            base.OnCreate();

            AppContext = this.ApplicationContext;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                PushNotificationManager.DefaultNotificationChannelId = "myEvents";
                PushNotificationManager.DefaultNotificationChannelName = "myChannel";
            }

            PushNotificationManager.SoundUri = Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{AppContext.PackageName}/raw/alarm");

            // this seems to get called
            CrossPushNotification.Current.OnTokenRefresh += (s, p) =>
            {
                System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
            };


    #if DEBUG
            PushNotificationManager.Initialize(this, true);
    #else
                  PushNotificationManager.Initialize(this,false);
    #endif
        }

并且此OnTokenRefresh不会:

    public partial class App : Application
{
    public new static App Current;
    public App()
    {
        InitializeComponent();
        Current = this;

        MainPage = new MainPage();
        var crossPushListener = CrossPushNotificationListener.Instance;
    }
}

CrossPushNotificationListener:

namespace MobileApp1
{
    public class CrossPushNotificationListener
    {
        private static CrossPushNotificationListener _instance;

        public static CrossPushNotificationListener Instance
        {
            get { return _instance ?? (_instance = new CrossPushNotificationListener()); }
            private set { _instance = value; }
        }

        public CrossPushNotificationListener()
        {
            CrossPushNotification.Current.OnTokenRefresh += OnTokenRefreshNotify;
        }

        // this function is not executed. Does that make sense?
        private async void OnTokenRefreshNotify(object sender, PushNotificationTokenEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine($"TOKEN : {args.Token}");
            Upload(args.Token);
        }
    }
}

只要应用程序正在运行,两者都可以工作。但是,如果应用程序关闭,则仅执行Android代码。

这有意义吗?

1 个答案:

答案 0 :(得分:0)

当应用程序运行时,一切运行正常,只是没有运行时。

似乎该服务只能在没有XamarinForms的情况下执行Android代码。 我们通过实施

解决了这个问题
<link rel="stylesheet" href="https://rawgit.com/utatti/perfect-scrollbar/master/css/perfect-scrollbar.css">
<div class="scroll">
  <div class="grid">
    <div class="headercell">Name</div>
    <div class="headercell">Long Column Header Age</div>
    <div class="headercell">Occupation</div>

    <div class="tablecell">Alex</div>
    <div class="tablecell">20</div>
    <div class="tablecell">Student</div>

    <div class="tablecell ">Chris</div>
    <div class="tablecell">20</div>
    <div class="tablecell">Human Being</div>

    <div class="tablecell ">Satoshi</div>
    <div class="tablecell">20</div>
    <div class="tablecell">Pokemon Trainer</div>

    <div class="tablecell ">Yong</div>
    <div class="tablecell">20</div>
    <div class="tablecell">Engineer</div>

    <div class="tablecell ">Long First Name...</div>
    <div class="tablecell">20</div>
    <div class="tablecell">Engineer</div>

    <div class="tablecell ">Name</div>
    <div class="tablecell">20</div>
    <div class="tablecell">A Really Long Occupation</div>
  </div>
</div>

<script src="https://rawgit.com/utatti/perfect-scrollbar/master/dist/perfect-scrollbar.js"></script>

在没有任何XamarinForms的Android中(因此也没有dependencyServices)。

感谢您的反馈。