xamarin iOS不可撤销的本地通知 [英] xamarin iOS non dismissible local notifications

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

问题描述

我有一个客户希望收到一个本地通知,告知用户该用户无法关闭,并且会一直存在,直到关闭该应用程序为止.我可以让它在android中工作,但不能在iOS中工作.我在xamarin中工作,并且不得不脱离pcl来完成此操作和一般的通知.这是可以实现的事情吗(苹果公司允许这样做)吗?还是我需要创建一个小部件扩展?

I have a client that wants to have a local notification that the user can not dismiss and will be there until the app is closed. I can get this to work in android but not in iOS. I am working in xamarin and have had to break out of the pcl to accomplish this and the notifications in general. Is this something that can be accomplished(does apple allow this)? Or is a widget extension what I am in need of creating?

推荐答案

您可以在运行的应用程序顶部显示本地通知通过实现 IUNUserNotificationCenterDelegate 并使用 UNNotificationPresentationOptions.Alert 选项调用 WillPresentNotification complementHandler.

You can present a local notification on top of your running app by implementing IUNUserNotificationCenterDelegate and calling the WillPresentNotification completionHandler with the UNNotificationPresentationOptions.Alert option.

但是您不能阻止用户将其关闭,因此将从通知中心将其清除.

But you can not prevent the user from dismissing it, thus is would be cleared from the Notification Center.

我不确定您要在永久"通知中显示哪些信息,但听起来像是应该查看今日视图"小部件.当然,缺点是今日"小部件要求用户手动将其添加到今日"视图中,并根据您的需要将其启用以用于锁定屏幕.

I'm not sure what information you are trying to show in the "permanent" notification, but it sounds like a Today View Widget is something you should look at. The downside of course is Today widgets require that the user manually adds the widget to the Today view and depending upon your needs that they enabled it for lock screen usage.

这是您执行本地通知并将其显示在应用程序顶部的方法....

Here is how you would do a local notification and make it present itself on top of your app....

[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
     completionHandler.DynamicInvoke( UNNotificationPresentationOptions.Alert );
}

分配UNUserNotificationCenter代表:

UNUserNotificationCenter.Current.Delegate = this;

发送本地通知:

var unAuthorizationOptions = UNAuthorizationOptions.Alert & UNAuthorizationOptions.Badge & UNAuthorizationOptions.Sound;
var authReply = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(unAuthorizationOptions);
if (authReply.Item1 == true) // Xamarin.iOS does not label the tuple members ;-( (granted, error) 
{
    var content = new UNMutableNotificationContent
    {
        Title = "Local Notification",
        Subtitle = "By SushiHangover",
        Body = "StackOverflow rocks",
        Badge = badgecount
    };
    var request = UNNotificationRequest.FromIdentifier("SushiHangover", content, null);
    await UNUserNotificationCenter.Current.AddNotificationRequestAsync(request);
}

这篇关于xamarin iOS不可撤销的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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