Xamarin 表单通知 [英] Xamarin Forms Notification

查看:103
本文介绍了Xamarin 表单通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to realize a shared project with Xamarin Forms for Android, ios and WinPhone. I want to use notifications and I followed this sample (https://github.com/edsnider/Xamarin.Plugins/tree/master/Notifier) and was able to create a local notification in Android. But i still do not understand how to work with that like Android application. Open a page or check if the page is already active or abort a broadcast for all platforms. Any links that could help me? Am I correct that it only works with an Interface that i have to implement in each platform or does exist any other solution?

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

My basic problem is that I get messages from the xmpp-protocol and want to send message notifications to the user or update the ui if open...

感谢您的帮助...

推荐答案

您不必为您的平台项目添加任何接口,只需将 nuget 包安装到两个 Android/IOS 项目中即可.

You do not have to add any interfaces to your platform projects, Just intall nuget package to both projects Android/IOS.

在IOS上你应该添加:

on IOS you should add:

// 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 中:

in AppDelegate:

然后在您的共享项目中调用:

Then in your shared project you call:

using Plugin.LocalNotifications.Abstractions;


    CrossLocalNotifications.Current.Show("You've got mail", "You have 793 unread messages!");

在替换当前页面时,您无需执行任何操作,Xamarin Forms 将为您处理.

As of replacing your current page, you do not have to do anything, Xamarin Forms will handle that for you.

同样在 android 上,您可以通过设置 MainActivity 的 LaunchMode 来控制应用程序重新启动行为:

Also on android, you can controll, you application relaunch behavior by setting LaunchMode of your MainActivity:

示例:

LaunchMode = Android.Content.PM.LaunchMode.SingleInstance, AlwaysRetainTaskState = true

要了解有关 Android 上 LauchModes 的更多信息,请阅读:

To read more about LauchModes on Android, please read:

https://developer.android.com/guide/components/tasks-and-back-stack.html

并检查其他活动标志:

FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP

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

Currently this plugin does not allow to catch any notification inside app. But you can do this, by using platform specific functionality

IOS:请参阅https://developer.xamarin.com/guides/cross-平台/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

Android: After reading through source of this plugin, i've discovered, that youcan check Bundle in your on create method for key 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.广播接收器/

Also you can try to use BroadcastReceiver class; https://developer.xamarin.com/api/type/Android.Content.BroadcastReceiver/

这篇关于Xamarin 表单通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆