Xamarin形式通知
我试图实现与Xamarin形式为Android,iOS和WinPhone共享项目。我想使用的通知,我跟着这个样本(https://github.com/edsnider/Xamarin.Plugins/tree/master/Notifier),并能创建Android的本地通知。但我仍然不明白如何使用Android应用程序。打开一个页面或检查页面是否已经处于活动状态或中止所有平台的广播。 任何可以帮助我的链接?我是否正确,它只适用于我必须在每个平台上实现的接口,还是存在任何其他解决方案?Xamarin形式通知
我的基本问题是,我从XMPP协议获取消息,并想开到消息通知发送给用户,或者更新UI ...
感谢您的帮助......
您不必为您的平台项目添加任何接口,只需将intget包安装到Android/IOS项目即可。
在IOS你应该增加:
// Ask the user for permission to get notifications on iOS 8.0+
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
var settings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}
给你的方法:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
中的AppDelegate
:
然后在你的共享项目中,您拨打:
using Plugin.LocalNotifications.Abstractions;
CrossLocalNotifications.Current.Show("You've got mail", "You have 793 unread messages!");
至于取代你的当前页面,你不需要做任何事情,Xamarin Forms将为你处理。
同样适用于Android,您可以通过设置您的MainActivity的LaunchMode控制研究,你的应用程序重新发布行为:
例子:
LaunchMode = Android.Content.PM.LaunchMode.SingleInstance, AlwaysRetainTaskState = true
要了解更多关于Android的LauchModes,请阅读:
https://developer.android.com/guide/components/tasks-and-back-stack.html
并检查添加活动标志:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
目前这款插件不允许内捕获应用程序的任何通知。 但你可以做到这一点,利用平台的特定功能
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
MessagingCenter.Send<MainPage> (this, "IOS notification received");
}
安卓 通过这个插件的源看完后,我发现,那遵守前面检查捆绑在您创建方法的密钥ScheduledAlarmHandler.LocalNotificationKey
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if(bundle.ContainsKey(ScheduledAlarmHandler.LocalNotificationKey)){
//App launched form notification
MessagingCenter.Send<MainPage> (this, "Android notification received");
}
}
此外,您可以尝试使用BroadcastRece iver类; https://developer.xamarin.com/api/type/Android.Content.BroadcastReceiver/
谢谢您的回答。我尝试过(Notifier.Current.Show(“你有邮件”,“你有793条未读消息!”));但是他在开始时却找不到Notifier。现在我试图再次通过控制台安装包,并且我得到了一个ItemNotFoundException。 任何想法?? – guenes
我已经编辑了一下示例,试试这个 – Greensy
非常感谢。 CrossLocalNotifications的作品。现在我会尝试你的其余部分。 – guenes