【问题标题】:Xamarin Forms NotificationXamarin 表单通知
【发布时间】:2016-10-11 10:24:33
【问题描述】:

我尝试用 Xamarin Forms for Android、ios 和 WinPhone 实现一个共享项目。我想使用通知,我按照这个示例 (https://github.com/edsnider/Xamarin.Plugins/tree/master/Notifier) 并能够在 Android 中创建本地通知。但我仍然不明白如何使用 Android 应用程序。打开页面或检查页面是否已处于活动状态或中止所有平台的广播。 任何可以帮助我的链接?我是否正确,它只适用于我必须在每个平台上实现的接口,还是存在任何其他解决方案?

我的基本问题是我从 xmpp 协议获取消息并希望向用户发送消息通知或在打开时更新 ui...

感谢您的帮助...

【问题讨论】:

    标签: xamarin notifications shared-project


    【解决方案1】:

    您不必为您的平台项目添加任何接口,只需将 nuget 包安装到两个项目 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
    

    目前此插件不允许在应用内捕获任何通知。 但是您可以通过使用特定于平台的功能来做到这一点

    IOS: 请参阅 https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/

      public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
      {
         MessagingCenter.Send<MainPage> (this, "IOS notification received");
      }
    

    安卓: 在阅读了这个插件的源代码后,我发现,你可以在你的 on create 方法中检查 Bundle 以获得关键的 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");
      }
    }
    

    你也可以尝试使用 BroadcastReceiver 类; https://developer.xamarin.com/api/type/Android.Content.BroadcastReceiver/

    【讨论】:

    • 感谢您的回答。我一开始就试过了 (Notifier.Current.Show("You've got mail", "You have 793 unread messages!");) 但他找不到 Notifier。现在我再次尝试通过控制台安装包,我得到了一个 ItemNotFoundException。有什么想法吗??
    • 非常感谢。 CrossLocalNotifications 有效。现在我会试试你剩下的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2018-03-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多