Xamarin IOS - 应用程序关闭时显示本地通知

Posted

技术标签:

【中文标题】Xamarin IOS - 应用程序关闭时显示本地通知【英文标题】:Xamarin IOS - Show local notification when application is closed 【发布时间】:2020-07-23 07:49:25 【问题描述】:

我有一个 Xamarin ios 应用程序,它每 10 秒获取一次用户位置,即使应用程序被终止也是如此。我让我们使用这个库:“https://jamesmontemagno.github.io/GeolocatorPlugin/”。

我想要的是:当应用程序关闭或打开并且用户位于特定位置时,我想显示本地通知。当应用程序关闭时,这甚至可能吗?我找不到这方面的信息,因为它总是关于远程通知。

【问题讨论】:

【参考方案1】:

应在应用启动后立即通过将以下代码添加到AppDelegateFinishedLaunching 方法并设置所需的通知类型(UNAuthorizationOptions)来请求通知权限:

...
using UserNotifications;
...



 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   
       ....
           
        //after iOS 10
        if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
        
            UNUserNotificationCenter center = UNUserNotificationCenter.Current;

            center.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.UNAuthorizationOptions.Badge, (bool arg1, NSError arg2) =>
                 

                 );

            center.Delegate = new NotificationDelegate();
        

        else if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        

            var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert| UIUserNotificationType.Badge| UIUserNotificationType.Sound,new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);

        

        return true;
    

iOS 10 的新增功能,当应用程序在前台并触发通知时,它可以以不同的方式处理通知。通过提供 UNUserNotificationCenterDelegate 并实现 UserNotificationCentermethod,应用程序可以接管显示通知的责任。例如:

using System;
using ObjCRuntime;
using UserNotifications;


namespace workplat

 public class NotificationDelegate:UNUserNotificationCenterDelegate
   
    public NotificationDelegate()
    
    

    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    
        // Do something with the notification
        Console.WriteLine("Active Notification: 0", notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
    


    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    
        // Take action based on Action ID
        switch (response.ActionIdentifier)
        
            case "reply":
                // Do something
                break;
            default:
                // Take action based on identifier
                if (response.IsDefaultAction)
                
                    // Handle default action...
                
                else if (response.IsDismissAction)
                
                    // Handle dismiss action
                
                break;
        

        // Inform caller it has been handled
        completionHandler();
    

  

要在系统中创建和注册自定义操作,请使用以下代码:

 public void RegisterNotification(long time)
    
        UNUserNotificationCenter center = UNUserNotificationCenter.Current;

        //creat a UNMutableNotificationContent which contains your notification content
        UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();

        notificationContent.Title = "xxx";
        notificationContent.Body= "xxxx";

        notificationContent.Sound = UNNotificationSound.Default;

        UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(time, false);

        UNNotificationRequest request = UNNotificationRequest.FromIdentifier("FiveSecond", notificationContent, trigger);


        center.AddNotificationRequest(request,(NSError obj) => 
        
           


        );

    

当你调用这个方法时,例如:

RegisterNotification(20);//set the time you want to push notification

通知将在 20 秒后推送,如果您关闭应用程序。您可以在上传位置后添加此行。

我的demo已经上传到我的github了,大家可以下载参考:Demo Link.

您可以访问链接以获取更多信息和详细信息:MicroSoft Document

【讨论】:

当应用关闭时,通知被标签时是否会触发 DidReceiveNotificationResponse? 是的,点击通知就会被调用

以上是关于Xamarin IOS - 应用程序关闭时显示本地通知的主要内容,如果未能解决你的问题,请参考以下文章

单击通知时显示调用本地通知的内容页面

iOS 10,当应用程序处于前台时显示本地通知?

键入时显示下拉菜单[关闭]

尝试在使用OAuth演示者时显示UINavigationController

用户关闭应用程序时如何处理用户在 Xamarin iOS 上单击本地通知?

应用打开时显示本机推送通知?