安排交互式 UILocalNotification - Obj-C

2024-03-08

我正在尝试安排一个互动UILocalNotifaction.

我的尝试是使用以下代码,这是我从中获取的tutorial https://nrj.io/simple-interactive-notifications-in-ios-8:

NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";

- (void)registerForNotification {

    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:@"Action 1"];
    [action1 setIdentifier:NotificationActionOneIdent];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:@"Action 2"];
    [action2 setIdentifier:NotificationActionTwoIdent];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2]
                    forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

但是,此代码似乎并未实际在任何地方安排通知。我缺少什么?


Apollo,

安排本地通知相对容易。 Piggy 退出了您引用的教程,我对它进行了切换,这样它就更有意义了,您可以根据需要阻止或更改代码,但这将为您提供比教程更好的起点。请注意,将其放入应用程序中并不是强制性的didFinishLaunchingWithOptions:你可以安排一个UILocalNotification应用程序范围内的任何地方。有关每个方法的完整属性,请参阅下面的参考资料

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

//Here we are just going to create the actions for the category. 
UIMutableUserNotificationAction *acceptAction = [self createAction];
UIMutableUserNotificationAction *laterAction = [self createLaterAction];
laterAction.title = @"Not Now";

//Creating the category and assigning the actions:
UIMutableUserNotificationCategory *acceptCategory = [self createCategory:@[acceptAction, laterAction]];

//Register the categories 
[self registerCategorySettings:acceptCategory];

//For testing purposes we will just fire a local notification on load. This is just for reference, but you don't have to call it in applicationDidFinishLaunching
[self fireLocalNotification];

return YES;
}

//Create a category
- (UIMutableUserNotificationCategory *)createCategory:(NSArray *)actions {
UIMutableUserNotificationCategory *acceptCategory = [[UIMutableUserNotificationCategory alloc] init];
acceptCategory.identifier = @"ACCEPT_CATEGORY";

[acceptCategory setActions:actions forContext:UIUserNotificationActionContextDefault];

return acceptCategory;
}

//Register your settings for that category 
- (void)registerCategorySettings:(UIMutableUserNotificationCategory *)category {
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);

NSSet *categories = [NSSet setWithObjects:category, nil];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

//Create Actions Methods
- (UIMutableUserNotificationAction *)createAction {

UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"ACCEPT_IDENTIFIER";
acceptAction.title = @"Accept";

acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;

// If YES requires passcode
acceptAction.authenticationRequired = NO;

return acceptAction;
}

- (UIMutableUserNotificationAction *)createLaterAction {

UIMutableUserNotificationAction *laterAction = [[UIMutableUserNotificationAction alloc] init];
laterAction.identifier = @"LATER_IDENTIFIER";
laterAction.title = @"Not Now";

laterAction.activationMode = UIUserNotificationActivationModeBackground;
laterAction.destructive = NO;
laterAction.authenticationRequired = NO;

return laterAction;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

UIUserNotificationType allowedTypes = [notificationSettings types];
NSLog(@"Registered for notification types: %u", allowedTypes);
}

//Fire a local Notification: For testing purposes we are just going to fire this immediately after launch. But this will give you a general idea of how to create a UILocalNotification app-wide:
- (void)fireLocalNotification {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"We are just testing -";
notification.category = @"ACCEPT_CATEGORY";

notification.fireDate = [NSDate dateWithTimeInterval:15 sinceDate:[NSDate date]];

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

And your outcome will be as anticipated: pic1

不要忘记为所选操作调用完成处理程序。application:handleActionWithIdentifier:forLocalNotification:completionHandler:

请查看下面的文档和参考资料以进行进一步的定制。再说一遍,我并不是说这是正确的方法,也绝对不是唯一的方法,这只是您了解它们如何工作的一个很好的起点。有许多可自定义的操作属性和UILocalNotifications


参考

  • UILocalNotification 和属性:文档 https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html
  • iOS8 通知操作和属性:文档 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIMutableUserNotificationAction_class/index.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

安排交互式 UILocalNotification - Obj-C 的相关文章

  • 将 MPMoviePlayerController 的视图添加到 UIView

    我想添加MPMoviePlayerController to a UIView 首先 我在 xib 文件中放置了一个视图 名为 youTubeView youtube播放器是MPMoviePlayerController youtubePl
  • Swift try inside Objective-C 块

    我需要创建一个函数foo它接受一个抛出闭包作为参数 我可以使用 Swift 或 ObjC 来实现它 但我需要能够从两者中调用它 像这样 Swift func bar throws func foo block throws gt void
  • 从 ios 应用程序到 symfony2 Web 应用程序的登录和管理会话

    我使用 Symfony2 开发了一个 Web 应用程序 我用过FOS用户包用于用户管理和安全 现在 我正在考虑为我的网络开发一个本机 iOS 应用程序 但并不真正知道如何解决从应用程序创建会话的问题 并在整个交互用户应用程序中维护它 换句话
  • 在 iOS 7 中查看 Core Data 创建的 sqlite 文件时出现问题

    当我尝试访问由核心数据创建的数据库文件时遇到问题 DB 文件位于 Documents 文件夹中 我已将其复制到桌面并使用 Firefox 插件打开它 Hello sqlite文件不包含任何行我有使用核心数据插入值 我正在使用 iOS 7 模
  • supportedInterfaceOrientations 方法不会重写其超类中的任何方法

    在 UIViewController 中 这段代码 public override func supportedInterfaceOrientations gt UIInterfaceOrientationMask if let mainC
  • iOS 确定视频中的帧数

    如果我有一个 Swift 中的 MPMoviePlayerController MPMoviePlayerController mp MPMoviePlayerController contentURL url 有没有办法获取视频中的帧数u
  • 如何使用逗号和行分隔符对字符串进行标记

    我正在 Swift 中制作一个简单的 String Tokenizer 就像在 Java 中一样 但这对我来说确实不起作用 我的数据源中每行的末尾用 分隔 数据用逗号分隔 例如 字符串 1 字符串 2 字符串 3 字符串 1 字符串 2 字
  • 与 parse-server 和 auth0 的自定义身份验证集成

    我想将 auth0 com 与开源解析服务器结合使用 我当前的方法是通过 iOS 的 Lock 库使用标准登录从 auth0 获取令牌 使用该令牌 我想在解析服务器上调用自定义身份验证方法 该方法检查令牌是否有效 如果有效则将登录用户 我的
  • 在 Xcode 中查找未使用的文件

    我最近开始开发一个新应用程序 它基本上是我以前制作的应用程序的副本 但做了一些更改 为了制作这个新应用程序 我复制了旧应用程序并删除了一些不需要的内容 我想知道 有没有办法知道 Xcode 中正在使用哪些类文件 或者有什么关于如何查找未使用
  • Bootstrap 响应式表格在 iOS 设备上无法垂直滚动

    这就是我所拥有的 div class table responsive table class table style background transparent table div 我正在使用以下 bootstrap css 文件 ht
  • iOS 7 NS 单线程安全合并冲突

    重新排序两行后 在单线程应用程序上保存简单的数据时遇到问题 我已经成功地简化了编码以重现错误 并且希望其他人尝试这一点时得到第二个意见 这是一次健全性检查 因为我怀疑 iOS 7 引入的核心数据问题 而这在 iOS 6 中工作正常 首先 启
  • UIScrollView 与 UITabBarController 切断

    我有一个 UIScrollView 我将其放置在视图中 界面生成器文档 xib m h 但是 UIScrollView 的下半部分被剪切 并且由于我有一个 UITabBarController 而没有显示其下半部分 我在 appdelega
  • iOS 有 INTERNET 权限吗?

    我在 iOS 设备上的 flutter dio 包上遇到了一个奇怪的问题 我编写了一个向 url 发送 GET 请求的应用程序 Android 上一切正常 但 iOS 上的请求似乎无法通过 没有发生任何错误 什么也没有 我在 Android
  • 使用标识符启动 iPhone 应用程序

    我正在尝试在我的应用程序中启动应用程序 例如 如果我按下 testApp1 中的按钮 它应该打开 testApp2 有没有办法用应用程序标识符来做到这一点 我听说过一种名为的未记录方法launchApplicationWithIdentif
  • Swift 单元测试 - 如何断言 CGColor 是它应该的样子?

    使用 Xcode V7 2 尝试进行单元测试 需要验证是否已设置正确的颜色 并收到以下消息 Cannot invoke XCTAssertEqual with an argument list of type CGColor CGColor
  • 将 NSFetchedResultsController 添加到项目后出现问题

    我设置 CoreData 时没有NSFetchedResultsController一切都保存得很好 切换到之后NSFetchedResultsController 我在尝试保存图像时遇到奇怪的错误 这是我用来保存图像的代码 void sa
  • dispatch_semaphore_t 重用 - 我在这里缺少什么?

    我有一些代码 其中使用dispatch semaphore t 来表示操作完成 当信号量是成员变量时 它的行为似乎不正确 我将展示有效的示例代码和似乎无效的示例代码 implementation someClass dispatch sem
  • 获取所有ios应用程序的全局列表[重复]

    这个问题在这里已经有答案了 我想对苹果应用商店进行一些全球统计 一个瓶颈是至少获取所有当前活动应用程序的 ID 这 9 位数字 有谁知道如何获取 iOS 应用商店中当前活动应用程序的所有 id 的完整列表 更好的是特定类别的所有 ID 例如
  • 带有自定义字体的 UILabel 错误呈现

    在我的 iPhone 应用程序中 我为所有 UILabel 设置了自定义字体 更准确地说 我对 UILabel 进行了子类化 重写了一个方法 在该方法中设置了自定义字体 然后将 IB 中的所有标签设置为该自定义类 现在的问题是 所有文本都渲
  • 从应用程序内发送电子邮件中的图像和文本

    如何从我的应用程序内通过电子邮件发送图像和文本 表格数据形式 请大家帮忙并提出建议 谢谢 void sendMailWithImage UIImage image if MFMailComposeViewController canSend

随机推荐

  • 在WPF中使用动画改变窗口大小

    我正在寻找一种方法来动画调整窗口大小 假设我有一个高度 300和宽度 300的窗口 我有2个按钮 当我单击第一个按钮时 窗口大小必须更改为高度 600并且宽度 600 当我单击另一个按钮时 窗口大小必须恢复到原始大小 我可以简单地更改高度和
  • 为什么错误处理程序不处理 Python/Flask 中包含尖号(“#”)符号的不存在 URL

    我正在构建一个 Python Flask 完全 ajax 无需重新加载整页 Web 应用程序 当 url 上的尖号 更改时 我更改了母版页的内容 本 阿尔曼 HashChange 事件 http benalman com projects
  • Spring - 在调用控制器的方法之前执行代码

    有没有类似的注释 PreAuthorize or PreFilter我可以用它在调用控制器中的方法之前运行代码吗 我需要将信息添加到请求上下文 特定于被调用的方法 然后由ExceptionHandler 例如 RestController
  • 如何使用 php if ($text contains "World") 搜索文本

    如何使用 php 搜索文本 就像是 除了更换if text contains World 具有工作条件 在你的情况下你可以只使用strpos http php net manual en function strpos php or str
  • 如何将十六进制字符串转换为 u8 切片?

    我有一个看起来像这样的字符串 090A0B0C 我想将其转换为看起来像这样的切片 9 10 11 12 我最好怎样做呢 我不想将单个十六进制字符元组转换为单个整数值 我想将由多个十六进制字符元组组成的字符串转换为多个整数值的切片 如果您想避
  • 如何在 UITableView 中像 iMessage iPhone 应用程序一样显示时间

    我在像 iMessage iPhone 应用程序一样滑动时无法在 UITableView 中显示时间 我已启用属性 显示水平滚动条 and 垂直弹跳 但它不能像 iMessage 应用程序一样正常工作 它需要压缩 UITableViewCe
  • Angular 无法使用 res.download 从 Express 获取文件下载

    在我的应用程序中 我在后端创建一个文件 然后我希望通过浏览器下载将其传递给用户 我已经尝试过无数次 这是 Express 后端 app get download req res gt res download filename txt fu
  • R中的指数曲线拟合

    time 1 100 head y 0 07841589 0 07686316 0 07534116 0 07384931 0 07238699 0 07095363 plot time y 这是一条指数曲线 在不知道公式的情况下如何在这条
  • 多态值类型和接口

    我有一个多态值类型 如下实现 class ShapeValue public template
  • 错误:CI_DB_mysql_result 类的对象无法转换为字符串

    我是 CodeIgniter 的新手 我尝试阅读 CI 的文档 但仍然无法解决我的问题 也许这里有人可以帮助解决我的问题 这是我的代码 在我的控制器中 class Registration extends CI Controller fun
  • 将第二个数据库添加到 alembic 上下文中

    我想在迁移过程中连接到第二个外部数据库 以将其部分数据移至本地数据库中 最好的方法是什么 将第二个数据库添加到 alembic 上下文后 我不知道该怎么做 在迁移期间如何在数据库上运行 SQL 语句 这就是我的env py现在看起来像 fr
  • 将多个变量从 HTML 传递到 PHP

    我想将两个变量传递到我的 php 页面 下拉变量工作得很好 但是当我添加一个附加变量时 它只发送 0 而不是我在表单中输入的内容 我觉得我已经非常接近这个问题的解决方案了 当我替换这一行上的数字时 xmlhttp open GET getd
  • Haskell:“Num [a] => a”和“Num a => [a]”之间有什么区别

    显然 我的类型签名已关闭 我现在已经知道原因了 现在 我有兴趣了解有关 GHCI 对我的拼写错误推断的签名的更多信息 我试图让这段代码工作 elemNum Eq a Num b gt a gt a gt b elemNum e l f e
  • 为什么我的 build.gradle android studio 中没有 allprojects{}?

    我正在开发一个 Android 应用程序 我需要将 PayPal 付款方式添加到该应用程序 所以我使用这个 https developer paypal com docs business native checkout android h
  • MariaDB Galera集群设置问题

    我正在尝试启动并运行 mariadb 集群 但它对我来说不起作用 现在我在 64 位 Red hat ES6 机器上使用 MariaDB Galera 5 5 36 我通过这里的存储库安装了 mariadb mariadb name Mar
  • pandas 切割多列

    我希望在多个列中应用一个容器 a 1 2 9 1 5 3 b 9 8 7 8 9 1 c a b print pd cut c 3 labels False 效果很好并创造了 0 0 2 0 1 0 2 2 2 2 2 0 但是 我想应用
  • 显示来自MySQL数据库的php中的所有表名

    好吧 我对 PHP 和 SQL MySQL 还很陌生 所以非常感谢您的帮助 我觉得我采取了正确的方法 我在 php net 上搜索 MySQL 显示所有表名称 它返回了一个已弃用的方法 并建议使用 MySQL 查询SHOW TABLES F
  • 如何更改Font Awesome感叹号三角形图标的内部白色?

    如何更改图标的内部 白色 颜色 i class fa fa exclamation triangle i 附 正在申请 i class fa fa exclamation triangle style color red i 不是答案 因为
  • 将 Admob 添加到 Libgdx 游戏

    我正在休憩本教程 https www youtube com watch v cwAN4LMXo58但是当我尝试进入货币化页面时 我无法货币化或添加任何横幅 因为与教程不同 我的游戏不在商店中 我的问题是 我是否需要在不添加 admob 代
  • 安排交互式 UILocalNotification - Obj-C

    我正在尝试安排一个互动UILocalNotifaction 我的尝试是使用以下代码 这是我从中获取的tutorial https nrj io simple interactive notifications in ios 8 NSStri