如何在我的应用程序加载但未在前台运行时发出通知? [英] How can I make my app send out notifications when it's loaded but not running in the foreground?

查看:73
本文介绍了如何在我的应用程序加载但未在前台运行时发出通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有时会使用的应用程序.我睡觉之前一定要将它留在后台.醒来时,我在屏幕上看到了此通知.

I have an app that I use sometimes. I must have left it there in the background before I slept. When I woke up I saw this notification on my screen.

有人对我如何在我的 XF 应用程序中显示这样的通知有任何建议吗?

Does anyone have any suggestions on how I can make a notification like this appear with my XF application?

此外,这些通知还会显示在 Android 上吗?我从来没有在我的 Android 手机上看到它们,但这可能是因为我使用它的次数少了.

Also, do these notifications appear on Android also? I've never seen them on my Android phone but that could be because I use it much less.

推荐答案

我们可以使用 Shiny .Notifications NuGet程序包,用于在Xamarin.Forms中创建跨平台的本地通知

We can use Shiny.Notifications NuGet Package to create cross-platform Local Notifications in Xamarin.Forms

使用以下代码创建的完整示例应用程序可以在此处找到: https://github.com/brminnick/LocalNotificationsSample

A completed sample app created using the code below can be found here: https://github.com/brminnick/LocalNotificationsSample

Shiny.Notifications NuGet包添加到您的Xamarin.Forms项目中,您的Xamarin.iOS项目和Xamarin.Android项目.

Add the Shiny.Notifications NuGet Package to your Xamarin.Forms project, your Xamarin.iOS project and Xamarin.Android project.

[Application]类的OnCreate中,通过调用Shiny.AndroidShinyHost.Init初始化Shiny并通过调用Shiny.Notifications.AndroidOptions.DefaultSmallIconResourceName设置其图标:

In the [Application] class, in OnCreate, initialize Shiny by calling Shiny.AndroidShinyHost.Init and setting its icon by calling Shiny.Notifications.AndroidOptions.DefaultSmallIconResourceName:

using System;
using Android.App;
using Android.Runtime;
using Shiny;

namespace LocalNotificationsSample.Droid
{
    [Application]
    public class YourApplication : Application
    {
        public YourApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            AndroidShinyHost.Init(this, platformBuild: services => services.UseNotifications());
            Notifications.AndroidOptions.DefaultSmallIconResourceName = "icon.png";
        }
    }
}

MainActivity.cs中的OnRequestPermission中,允许Shiny通过添加Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);

In MainActivity.cs, in OnRequestPermission, allow Shiny to present request notifications permissions from the user by adding Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;

namespace LocalNotificationsSample.Droid
{
    [Activity(Label = "LocalNotificationsSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }
}

iOS

AppDelegate.cs中的FinishedLaunching中,通过调用Shiny.iOSShinyHost.Init初始化Shiny:

iOS

In AppDelegate.cs, in FinishedLaunching, initialize Shiny by calling Shiny.iOSShinyHost.Init:

using Foundation;
using UIKit;
using Shiny;

namespace LocalNotificationsSample.iOS
{
    [Register(nameof(AppDelegate))]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            iOSShinyHost.Init(platformBuild: services => services.UseNotifications());
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }
    }
}

3.安排本地通知

在此示例中,我们将立即发送本地通知,并安排在应用启动后一分钟发送一次本地通知.

3. Schedule a Local Notification

In this example, we will send a Local Notification immediately and schedule one to be sent one minute after the app launches

using System;
using System.Threading.Tasks;
using Shiny;
using Shiny.Notifications;
using Xamarin.Forms;

namespace LocalNotificationsSample
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

        protected override async void OnStart()
        {
            await SendNotificationNow();
            await ScheduleLocalNotification(DateTimeOffset.UtcNow.AddMinutes(1));
        }

        Task SendNotificationNow()
        {
            var notification = new Notification
            {
                Title = "Testing Local Notifications",
                Message = "It's working",
            };

            return ShinyHost.Resolve<INotificationManager>().RequestAccessAndSend(notification);
        }

        Task ScheduleLocalNotification(DateTimeOffset scheduledTime)
        {
            var notification = new Notification
            {
                Title = "Testing Local Notifications",
                Message = "It's working",
                ScheduleDate = scheduledTime
            };

            return ShinyHost.Resolve<INotificationManager>().Send(notification);
        }
    }
}

https://github.com/brminnick/LocalNotificationsSample

这篇关于如何在我的应用程序加载但未在前台运行时发出通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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