简体   繁体   English

Xamarin.Forms AppCenter推送通知

[英]Xamarin.Forms AppCenter Push Notification

Currently, I am using Xamarin.Forms with AppCenter Push, in other, to have notifications. 目前,我正在将Xamarin.Forms与AppCenter Push一起使用,以获取通知。 My end goal is to have this functionality: 我的最终目标是拥有以下功能:

If the app is closed OR opened I can receive a notification (listed inside notifications list) and on click, redirect the user to a certain page. 如果应用已关闭或打开,我会收到一条通知(在通知列表中列出),然后单击将用户重定向到某个页面。 Based on the custom data provided, I am going to redirect him to a different page. 根据提供的自定义数据,我将把他重定向到另一个页面。 What I've found in AppCenter Push docs for Xamarin.Forms are that if the app is opened, my method should have the new notification event, but it will not - display the notification. 在AppCenter Push文档中的Xamarin.Forms中发现,如果打开应用程序,则我的方法应该具有新的通知事件,但不会-显示通知。

Here is my very simple code: App.xaml.cs 这是我非常简单的代码: 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));
}

Then I have: MainActivity.cs 然后我有: MainActivity.cs

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

Struggling to implement this simple scenario I have faced different problems: 努力实现这一简单方案,我遇到了不同的问题:

  1. When I first start the app and send a notification (while the app is opened), the handler of "PushNotificationReceived" it's not hit. 当我第一次启动应用程序并发送通知(在应用程序打开时)时,“ PushNotificationReceived”的处理程序没有被点击。 Then I leave the app in the background and send again a notification. 然后,我将应用程序保留在后台,并再次发送通知。 Now this time I receive the notification inside my "Notifications List" on Android and when I click it, the app is opening, then it goes through "OnNewIntent" and then it's hitting "PushNotificationReceived". 现在,这次我在Android上的“通知列表”中收到通知,当我单击该应用时,该应用正在打开,然后经过“ OnNewIntent”,然后单击“ PushNotificationReceived”。 Every notification after that is doing the same thing (so it's working properly) no matter if the app is running on foreground or background. 无论应用程序是在前台还是后台运行,此后的每个通知都将执行相同的操作(因此它可以正常工作)。 To summarize, the problem is that first time when I start the app, I don't receive notifications and I receive them only if I minimize it and the open a new notification, so I go through the method "OnNewIntent". 总而言之,问题在于,当我第一次启动该应用程序时,我没有收到通知,只有将其最小化并打开一个新通知时,我才会收到通知,因此我会使用“ OnNewIntent”方法。
  2. The second scenario is how can I know if "PushNotificationReceived" has been triggered while the app is working in the background or foreground, based on that I want to run different logic. 第二种情况是,基于我想运行不同的逻辑,如何知道应用在后台或前台工作时是否触发了“ PushNotificationReceived”。 Because apparently, AppCenter will not show a notification if the app is opened, then I want to create my own, but in that case, I don't know was the app opened while notification came or not. 因为显然,如果打开应用程序,AppCenter不会显示通知,那么我想创建自己的通知,但是在那种情况下,我不知道在通知到来时是否打开了该应用程序。

If you need any more information, please do let me know! 如果您需要更多信息,请告诉我! Thanks in advance! 提前致谢!

Try doing the notifications events in the android. 尝试在android中执行通知事件。 To pass the parameters from Android to Forms App, you can use MessagingCenter. 要将参数从Android传递到Forms App,可以使用MessagingCenter。 In the below, notification received event, you can extract the data from the notification payload and pass to forms app using MessagingCenter. 在下面的通知接收事件中,您可以从通知有效负载中提取数据,然后使用MessagingCenter传递给表单应用程序。 Ensure you have done the below steps.. 确保您已完成以下步骤。

In the OnCreate method of your MainActivity, register and subscribe for PushNotification. 在MainActivity的OnCreate方法中,注册并订阅PushNotification。

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

    };
  }

In the NewIntent method, 在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