Xamarin.Forms AppCenter当应用程序在后台或终止时推送

时间:2019-07-08 06:19:30

标签: xamarin.forms push-notification xamarin.android xamarin.ios visual-studio-app-center

我正在使用Xamarin.Forms实现应用程序,并定位到Xamarin.Android和Xamarin.iOS。我目前正在尝试从AppCenter添加“推送”功能。我已按照https://docs.microsoft.com/en-us/appcenter/sdk/push/xamarin-forms上的说明进行操作,并在App.xaml.cs

中添加了以下代码
protected override void OnStart()
    {
        // Handle when your app starts

        // This should come before AppCenter.Start() is called
        // Avoid duplicate event registration:
        if (!AppCenter.Configured)
        {
            Push.PushNotificationReceived += async (sender, e) =>
            {
                // Add the notification message and title to the message
                var summary = $"Push notification received:" +
                                    $"\n\tNotification title: {e.Title}" +
                                    $"\n\tMessage: {e.Message}";

                // If there is custom data associated with the notification,
                // print the entries
                if (e.CustomData != null)
                {
                    summary += "\n\tCustom data:\n";
                    foreach (var key in e.CustomData.Keys)
                    {
                        summary += $"\t\t{key} : {e.CustomData[key]}\n";
                    }
                }

                await Application.Current.MainPage.DisplayAlert(e.Title, e.Message, MobileLibrary.Resources.Localization.txtOK);

                // Send the notification summary to debug output
                System.Diagnostics.Debug.WriteLine(summary);
            };
        }

        AppCenter.Start("ios=XXX;android=XXX", typeof(Analytics), typeof(Crashes), typeof(Push));
    }

目前,我正在 Android 上进行测试。

当应用程序处于前台状态时,我毫无问题地接收通知并在var summary处收到消息。我现在面临的问题是

1)当应用在后台运行时,我收到通知。当我单击它时,它将进入PushNotificationReceived事件,但是e.Titlee.Message为空。

  

答案:将所需的数据放入CustomData中   将得到维护。

2)如果我终止了该应用程序,则通知将根本不会出现。

摘要

  1. 当应用程序为前台时-通知(NO);消息和标题(是)
  2. 当应用程序为后台时-通知(是);邮件和标题(否)
  

答案:在Android中,放入CustomData以保留所需的数据。

  1. 应用被杀死时-通知(NO);消息和标题(否)

我的问题是

1)我在Android(Java)上所做的是我需要实现Service并扩展GcmListenerService。 Xamarin.Forms / Xamarin.Android是否需要类似的东西?

2)Xamarin.iOS怎么样? (我尚未开始对其进行测试)在那该怎么办?

谢谢。

0 个答案:

没有答案
相关问题