繁体   English   中英

Xamarin.Forms AppCenter推送通知

[英]Xamarin.Forms AppCenter Push Notification

目前,我正在将Xamarin.Forms与AppCenter Push一起使用,以获取通知。 我的最终目标是拥有以下功能:

如果应用已关闭或打开,我会收到一条通知(在通知列表中列出),然后单击将用户重定向到某个页面。 根据提供的自定义数据,我将把他重定向到另一个页面。 在AppCenter Push文档中的Xamarin.Forms中发现,如果打开应用程序,则我的方法应该具有新的通知事件,但不会-显示通知。

这是我非常简单的代码: App.xaml.cs

protected override async void OnStart()
{
    await Push.SetEnabledAsync(true);
    if (!AppCenter.Configured)
    {
        Push.PushNotificationReceived += (sender, e) =>
        {
            DependencyService.Resolve<IMessageService>().LongAlert("TESTT");
            // Add the notification message and title to the message
            string 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 (string key in e.CustomData.Keys)
                {
                    summary += $"\t\t{key} : {e.CustomData[key]}\n";
                }
            }

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

    AppCenter.Start("", typeof(Push));
}

然后我有: MainActivity.cs

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    Push.CheckLaunchedFromNotification(this, intent);
}

努力实现这一简单方案,我遇到了不同的问题:

  1. 当我第一次启动应用程序并发送通知(在应用程序打开时)时,“ PushNotificationReceived”的处理程序没有被点击。 然后,我将应用程序保留在后台,并再次发送通知。 现在,这次我在Android上的“通知列表”中收到通知,当我单击该应用时,该应用正在打开,然后经过“ OnNewIntent”,然后单击“ PushNotificationReceived”。 无论应用程序是在前台还是后台运行,此后的每个通知都将执行相同的操作(因此它可以正常工作)。 总而言之,问题在于,当我第一次启动该应用程序时,我没有收到通知,只有将其最小化并打开一个新通知时,我才会收到通知,因此我会使用“ OnNewIntent”方法。
  2. 第二种情况是,基于我想运行不同的逻辑,如何知道应用在后台或前台工作时是否触发了“ PushNotificationReceived”。 因为显然,如果打开应用程序,AppCenter不会显示通知,那么我想创建自己的通知,但是在那种情况下,我不知道在通知到来时是否打开了该应用程序。

如果您需要更多信息,请告诉我! 提前致谢!

尝试在android中执行通知事件。 要将参数从Android传递到Forms App,可以使用MessagingCenter。 在下面的通知接收事件中,您可以从通知有效负载中提取数据,然后使用MessagingCenter传递给表单应用程序。 确保您已完成以下步骤。

在MainActivity的OnCreate方法中,注册并订阅PushNotification。

 if (!AppCenter.Configured)
 {
    Push.PushNotificationReceived += (sender, e) =>
    {

    };
  }

在NewIntent方法中,

protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        Push.CheckLaunchedFromNotification(this, intent);

        var type = intent.GetStringExtra("type");
        if ((!string.IsNullOrEmpty(type) && object.Equals(type, "notification")))
        {

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM