iOS 中的 UILocalNotification 中不显示操作和类别

2023-11-29

我相信苹果文档中有以下内容here我只需要一个类别UILocalNotification:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    UIMutableUserNotificationAction *acceptAction =
    [[UIMutableUserNotificationAction alloc] init];

    // Define an ID string to be passed back to your app when you handle the action
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";

    // Localized string displayed in the action button
    acceptAction.title = @"Accept";

    // If you need to show UI, choose foreground
    acceptAction.activationMode = UIUserNotificationActivationModeBackground;

    // Destructive actions display in red
    acceptAction.destructive = NO;

    // Set whether the action requires the user to authenticate
    acceptAction.authenticationRequired = NO;


    // First create the category
    UIMutableUserNotificationCategory *inviteCategory =
    [[UIMutableUserNotificationCategory alloc] init];

    // Identifier to include in your push payload and local notification
    inviteCategory.identifier = @"INVITE_CATEGORY";

    // Add the actions to the category and set the action context
    [inviteCategory setActions:@[acceptAction]
                    forContext:UIUserNotificationActionContextDefault];

    // Set the actions to present in a minimal context
    [inviteCategory setActions:@[acceptAction]
                    forContext:UIUserNotificationActionContextMinimal];

    NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];



    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    // Handle launching from a notification
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        NSLog(@"Recieved Notification %@",localNotif);
    }

    return YES;
}

以下是我构建本地通知的方法:

NSDate *dateChosen = [self.reminderDatePicker date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:dateChosen];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];

//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay: 3];
    [components setMonth: 7];
    [components setYear: 2012];
    [components setHour: hour];
    [components setMinute: minute];
    [components setSecond: 0];
    [calendar setTimeZone: [NSTimeZone defaultTimeZone]];
    NSDate *dateToFire = [calendar dateFromComponents:components];



    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.fireDate = dateToFire;

            [localNotification setRepeatInterval: kCFCalendarUnitDay];
            NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);
            localNotification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:self.dayPeriod, @"name", nil];
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = alertMessage;
            localNotification.alertAction = NSLocalizedString(@"View details", nil);
            localNotification.repeatInterval = NSDayCalendarUnit;
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            localNotification.applicationIconBadgeNumber = -1;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

它只显示带有关闭按钮的默认通知,而不是我的接受按钮。

如何让此“接受”按钮显示在我的本地通知上?


您在代码主体中缺少的一件事UILocalNotification明确告诉它是基于类别的通知。

将类别属性添加到您的通知中:

localNotification.category = @"INVITE_CATEGORY";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 中的 UILocalNotification 中不显示操作和类别 的相关文章

随机推荐

  • 使用并行有线 RS232 从多个设备接收数据

    我目前正在开发一个小型应用程序 用于监控太阳能集热器产生的功率 电流 该阵列连接到 3 个逆变器 每台逆变器都有一个RS232接口 每10秒传输一行信息 当前状态 由于我想使用只有一个串行端口的设备进行监控 因此我需要想出一种能够并行读取所
  • Windows Phone 7:文件流异常

    我尝试使用 FileStream 使用命名空间 System IO 但出现异常 Attempt to access the method failed 这是代码 FileStream fs new FileStream file txt F
  • 如何使用 apache POI XWPFDocument 从 java 创建多级项目符号和编号?

    我已经阅读了许多与我的要求相关的博客和论坛 但到目前为止 我能够在获得的所有帮助下生成第一级的项目符号或编号 任何人都可以指导我如何使用 apache poi 创建多级编号 想知道 Apache POI XWPFDocument 是否支持这
  • 使用 KSOAP Android 创建 SOAP 请求

    我需要生成一个像这样的肥皂请求 SOAP请求 POST TennisMasters TennisMasters Listener asmx HTTP 1 1 Host playinkstudio com Content Type text
  • 使用 mod_rewrite 从子域重定向到主域

    我的问题是我有一个正常运行的子域 sub mydomain com sub 已加载到 mydomain com sub 中 我想做的是将所有对 sub mydomain com 的请求重定向到 mydomain com 不知怎的 当我是子域
  • SQL 过程 XML 性能:插入表中的列

    我在 SQL 过程中遇到问题 但似乎找不到正确的解决方案 该存储过程包含一个 XML 数据类型的参数 名称 data 传入消息的示例如下 实际消息包含更多节点 但为了简单起见 我将它们省略了
  • 使用 GDI+ API 绘制图像

    我正在使用GDI API 只是为了显示图像 bmp 屏幕上 这是执行任务的函数的代码 void drawImg ArmorInfo PupupWnd HDC hdc GdiplusStartupInput gdiplusStartupInp
  • 如何在MainActivity中获取ViewPager创建的fragment的元素?

    我有几个片段 它们是用ViewPager 我添加一个addOnPageChangeListener to the ViewPager在 MainActivity 中onCreate给出所选片段位置的方法 尽管我通过声明防止碎片自我毁灭set
  • 使用 async/await 时 GUI 冻结

    我试图找出下面的代码有什么问题 我认为使用 async 和 wait 让我忘记 GUI 问题 例如冻结 因为一些长代码阻塞了主线程 单击按钮后 GUI 会做出响应 直到调用longRunningMethod 如下所示 private asy
  • 保护远程 mysql 连接的安全

    我的处境很不幸 必须将本地 microsoft access 数据库与远程 mysql 数据库同步 我编写了一个 php 脚本 它将每 10 分钟同步一次数据库 但我绝对担心安全问题 到目前为止 我已经使用 cpanel 设置了远程 mys
  • 'NoneType' 对象在 BeautifulSoup 中没有属性 'text'

    当我搜索 时 我试图抓取谷歌结果 什么是2 2 但返回以下代码 NoneType object has no attribute text 请帮助我实现所需的目标 text What is 2 2 search text replace l
  • 如何处理for循环中路径名中的空格?

    尝试使用当前脚本的路径 并且该路径中包含空格 但我似乎无法让它工作 C Test Directory gt dir Volume in drive C has no label Volume Serial Number is 7486 CE
  • 自动滚动到页面底部然后顶部并重复

    我创建了一个仪表板 它会回显所有行 但我想要一个自动滚动 它会慢慢地向下滚动到页面底部 然后慢慢地滚动回顶部并重复 我已经用谷歌搜索了这个 但无法获得有效的代码 我一直在寻找 jQuery 代码 但任何东西都可以 因为页面会一直调整 所以不
  • 在 C++ 中从给定的种子生成相同的随机数序列

    我正在使用 mt19937 从给定种子生成随机字符串 如下所示 std string StringUtils randstring size t length uint64 t seed static auto chrs abcdefghi
  • 单击react-native中的菜单项时如何打开页脚选项卡屏幕之一?

    我正在创建一个反应本机应用程序 现在我想在单击菜单项时打开页脚选项卡屏幕之一 在这里 我附上了我的示例屏幕 可以从中清楚地了解我的问题 这是我的 GitHub 存储库 https github com SidathDananjaya Eve
  • 使用 C++ 编程时,如何在 win32 中创建子窗口?

    我是 C 以及 Windows 编程的新手 我使用 msdn 创建了一个窗口CreateWindow 功能 其工作正常 现在我想创建一个子窗口 父窗口应该控制子窗口 任何有关此的帮助示例代码 提前致谢 粗略地说 在父级的处理程序中 您希望创
  • 数组的元素类型不完整。这是什么意思? [复制]

    这个问题在这里已经有答案了 我想创建一个数组来存储任何整数 n 的乘法值 之后 我想将该数组传递给另一个函数并打印出该数组 但是 我收到以下错误 My code 这是我的 c 文件 include multiplication h incl
  • Windows 上的 LDAP 身份验证

    我安装了 64 位 Windows 平台和 Postgresql 8 4 作为 Apache EnterpriseDB ApachePHP 安装在 Pstgres 上 我需要使用 Active Directory 或 LDAP 凭据对系统进
  • 使用 C# 从另一个表单获取值[重复]

    这个问题在这里已经有答案了 如何从一种形式获取另一种形式的值 我已经尝试过以下方法 但它不起作用 Form1 public TextBox TXT get return txtbox1 Form2 public Form1 frm txtb
  • iOS 中的 UILocalNotification 中不显示操作和类别

    我相信苹果文档中有以下内容here我只需要一个类别UILocalNotification BOOL application UIApplication application didFinishLaunchingWithOptions NS