【问题标题】:Xamarin android: Async await calls not working when app open by clicking push notificationXamarin android:通过单击推送通知打开应用程序时异步等待调用不起作用
【发布时间】:2018-02-02 20:55:27
【问题描述】:

当我通过点击 FCM 推送通知打开应用程序时,我使用 await 关键字进行的 API 服务调用不起作用。整个应用程序不返回数据。 API调用代码

var result = await objHomework.GetHomeWorksForStudentPagesAsync(studentId.ToString());

结果返回null。如果应用程序已经打开,一切正常。见下图screenshot of app

【问题讨论】:

    标签: firebase xamarin.android async-await firebase-cloud-messaging


    【解决方案1】:

    仅当应用程序处于前台时,通知消息才会传递到OnMessageReceived 回调。

    覆盖FirebaseMessageServiceHandleIntent 方法也适用于后台

    public override void HandleIntent(Intent intent)
    {
        try
        {
            if (intent.Extras != null)
            {
                var builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
    
                foreach (string key in intent.Extras.KeySet())
                {
                    builder.AddData(key, intent.Extras.Get(key).ToString());
                }
    
                this.OnMessageReceived(builder.Build());
            }
            else
            {
                base.HandleIntent(intent);
            }
        }
        catch (Exception)
        {
            base.HandleIntent(intent);
        }
    }
    

    【讨论】:

    • @iamlcarus,OnMessageReceived 每次都为我打电话。但问题是当我通过点击通知打开前台应用程序时,应用程序正在打开特定屏幕但没有任何工作,例如没有从服务中获取数据。
    【解决方案2】:

    实际上,我在项目中缺少一些服务调用身份验证所必需的密钥。我在 MaiActivity 中获取了这些键,但通知单击甚至从其他地方启动应用程序,因此键值为 null 并且没有发生服务调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多