Xamarin.Forms AppCenter 推送通知
Xamarin.Forms AppCenter Push Notification
目前,我正在将 Xamarin.Forms 与 AppCenter Push 一起使用,以接收通知。我的最终目标是拥有此功能:
如果应用程序关闭或打开,我可以收到通知(列在通知列表中)并在点击时将用户重定向到特定页面。根据提供的自定义数据,我将把他重定向到另一个页面。我在 Xamarin.Forms 的 AppCenter Push 文档中发现,如果应用程序打开,我的方法应该有新的通知事件,但它不会 - 显示通知。
这是我非常简单的代码:
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);
}
努力实现这个简单的场景,我遇到了不同的问题:
- 当我第一次启动应用程序并发送通知时(当应用程序打开时),"PushNotificationReceived" 的处理程序未命中。然后我将应用程序留在后台并再次发送通知。现在这一次我在 Android 上收到 "Notifications List" 中的通知,当我单击它时,应用程序正在打开,然后它经过 "OnNewIntent" 然后点击 "PushNotificationReceived"。此后的每个通知都在做同样的事情(因此它工作正常),无论该应用程序是 运行 在前台还是后台。总而言之,问题是当我第一次启动应用程序时,我没有收到通知,只有当我最小化它并打开一个新通知时我才会收到通知,所以我通过方法 "OnNewIntent".
- 第二种情况是我想运行不同的逻辑,如何知道应用程序在后台或前台运行时是否触发了"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")))
{
}
}
目前,我正在将 Xamarin.Forms 与 AppCenter Push 一起使用,以接收通知。我的最终目标是拥有此功能:
如果应用程序关闭或打开,我可以收到通知(列在通知列表中)并在点击时将用户重定向到特定页面。根据提供的自定义数据,我将把他重定向到另一个页面。我在 Xamarin.Forms 的 AppCenter Push 文档中发现,如果应用程序打开,我的方法应该有新的通知事件,但它不会 - 显示通知。
这是我非常简单的代码: 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);
}
努力实现这个简单的场景,我遇到了不同的问题:
- 当我第一次启动应用程序并发送通知时(当应用程序打开时),"PushNotificationReceived" 的处理程序未命中。然后我将应用程序留在后台并再次发送通知。现在这一次我在 Android 上收到 "Notifications List" 中的通知,当我单击它时,应用程序正在打开,然后它经过 "OnNewIntent" 然后点击 "PushNotificationReceived"。此后的每个通知都在做同样的事情(因此它工作正常),无论该应用程序是 运行 在前台还是后台。总而言之,问题是当我第一次启动应用程序时,我没有收到通知,只有当我最小化它并打开一个新通知时我才会收到通知,所以我通过方法 "OnNewIntent".
- 第二种情况是我想运行不同的逻辑,如何知道应用程序在后台或前台运行时是否触发了"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")))
{
}
}