c# - Xamarin iOS 中的可操作推送通知
问题描述
var acceptAction = UNNotificationAction.FromIdentifier("AcceptAction", "Accept", UNNotificationActionOptions.None);
var declineAction = UNNotificationAction.FromIdentifier("DeclineAction", "Decline", UNNotificationActionOptions.None);
// Create category
var meetingInviteCategory = UNNotificationCategory.FromIdentifier("MeetingInvitation",
new UNNotificationAction[] { acceptAction, declineAction }, new string[] { }, UNNotificationCategoryOptions.CustomDismissAction);
// Register category
var categories = new UNNotificationCategory[] { meetingInviteCategory };
UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
您如何接收自定义的可操作推送通知以及需要将上述代码放在哪个文件中?
解决方案
在 iOS 应用程序可以向用户发送通知之前,该应用程序必须在系统中注册,并且由于通知对用户来说是一种中断,因此应用程序必须在发送它们之前明确请求权限。
通过将以下代码添加到AppDelegate的FinishedLaunching方法并设置所需的通知类型(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 并实现 UserNotificationCenter 方法,应用程序可以接管显示通知的责任。例如:
using System;
using ObjCRuntime;
using UserNotifications;
namespace xxx
{
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();
}
}
}
推荐阅读
- c++ - /usr/share/cmake-3.5/.. 处的 CMake 错误 C++ 编译器无法编译简单的测试程序
- metadata - 有没有办法自动访问 Tableau 中的元数据?
- jquery - 如何只附加到每个 div 一次
- actions-on-google - 为什么我(有时)在使用数字商品样本时会收到 TypeError?
- python - Python/Pandas 样式的列标题
- java - Mockito 无法注入模拟
- javascript - 显示具有来自 json 的真值的复选框
- html - 在哪里轻松存储自定义标签助手的 HTML 内容?
- python - Python,Selenium:从返回列表中分离项目
- free-jqgrid - formatter:'select' 不显示元素的描述,而是显示选项的值