安排交互式 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 的相关文章

  • 如何在IOS中的UIStackView中设置权重

    UIStackView与安卓类似LinearLayout但我不知道如何设置子视图的权重 假设我有一个垂直的UIStackView and 3 UIImageView就在里面 我想连续设置权重3 6 1UIImageViews 我怎么做 UI
  • 如何使用 iPhone SDK 实现可滑动的图像堆栈(例如照片应用程序)?

    我想获取一堆图像 或者可能是一组用于下载图像的 URL 并以全屏方式显示它们 一次一个 使用 iPhone SDK 使用用户滑动来平滑地为堆栈中的下一个图像设置动画 Apple 的 Photo app 似乎可以做到这一点 此外 如果尚未检索
  • 修补应用内购买黑客;卡在第四步

    正如我们许多人所知 苹果最近出现了一种情况 黑客可以免费获得任何应用内购买 苹果最近发布了这个文件 http developer apple com library ios releasenotes StoreKit IAP Receipt
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • 从 UIImagePickerController 相机视图推送 viewController

    我正在开发一款消息应用程序 类似于 WhatsApp 用户可以互相发送文本和图像消息 当用户想要发送图像时 他可以从相机胶卷中选择一张图像 也可以用相机拍摄一张图像 这就是我介绍的方式UIImagePickerController对于这两种
  • 网站在 iPhone 屏幕右侧显示空白区域

    我遇到问题http eiglaw com http eiglaw com iPhone 屏幕右侧显示约 25 像素宽的空白 边框 我在 stackoverflow 上研究了这个问题 这些帖子是相关的 但是当我尝试提供的各种解决方案时 我无法
  • 使用 iPhone 中的地图视图读取当前位置名称

    我读取了当前位置的纬度和经度值 然后成功将该位置固定在 iPhone 中 现在我想使用这个纬度和经度值读取该地名 我使用以下代码来读取查找当前位置 void mapView MKMapView mapView1 didUpdateUserL
  • 如何在 Firebase 控制台中使用 Apple 新的 APN .p8 证书

    随着最近 Apple 开发者帐户的升级 我面临着一个困难 在尝试创建推送通知证书时 它为我提供了 p8 证书 而不是可以导出到 p12 的 APNs 证书 Firebase 控制台仅接受 p12 证书 那么我如何从这些新的 p8 证书中获取
  • 带操作按钮的颤动本地通知

    我在我的 flutter 项目中尝试了 flutter 本地通知插件 它在简单通知上工作正常 但我需要带有操作按钮的通知功能 请帮助我或建议我实现此功能 不幸的是 flutter local notifications 插件尚不支持操作按钮
  • 当地图视图只是屏幕的一部分时,如何在 iOS 模拟器中进行捏合?

    我在 iPad 上有一个视图 我正在添加MKMapView也就是说 全屏高度的一半 然而 当我尝试在 iOS 模拟器上进行捏合时 它不起作用 因为 to nubs 填充了模拟器上的整个 iPad 视图 And so with the map
  • 是否可以使用 Firebase 安排推送通知? [复制]

    这个问题在这里已经有答案了 我已经阅读了我能找到的所有文档 但仍然不知道这是否可行 如果我是用户 我可以安排特定时间的推送通知吗 Example 1 我是用户并打开应用程序 2 我允许通知并转到 pickerView 或其他任何内容 并设置
  • 更改组织以使用 Xcode 9 在 iTunes Connect 上上传二进制文件

    我在 Xcode9 上配置了多个团队 当我尝试将二进制文件上传到 Xcode 9 上的 iTunes Connect 时 没有更改团队的选项 并且出现以下错误 ERROR ITMS 4088 来自苹果开发者论坛的解决方案 1 正常存档2 窗
  • 适用于 iPhone / iPad / iOS 的快速、精益 PDF 查看器 - 提示和提示?

    最近有很多关于绘制 PDF 的问题 是的 您可以使用UIWebView但这无法提供您所期望的优秀 PDF 查看器的性能和功能 您可以绘制PDF页面到 CALayer http www cocoabuilder com archive coc
  • 在 iOS 上将 NSString 转换为 NSDate 的正确方法?

    我一直在使用此方法将常规 NSString 对象转换为 NSDate 但尝试向 Apple 提交更新 但遭到拒绝 在 iOS 中还有什么其他方法可以做到这一点 NSString date str 2011 08 12T12 20 00Z N
  • UIButton的高亮状态由什么控制事件开始和结束

    我正在创建类似钢琴的视图UIButton作为钢琴键 什么UIControlEvents当按钮获得和失去突出显示状态时 我应该监听以获得回调吗 我试图创建子类UIButton并添加属性观察者highlighted并且运行良好 然而 有时我需要
  • 覆盖层不与 UITableView 一起滚动 - iOS

    我有一个 UITableView 类 它使用以下方法在转到下一个屏幕时调用加载覆盖 问题是这个加载屏幕不随列表滚动 所以如果你滚动一点并单击某些东西 加载屏幕不会显示 因为它位于顶部 如何让加载屏幕始终保持在 UITableView 的顶部
  • 如何使用 Swift 使用 TouchID?

    Apple 为 iOS 8 的 TouchID 实现提供的文档采用 Objective C 语言 有 Swift 版本吗 Objective C IBAction touchIDAvailable UIButton touchIDAvail
  • 水平 UICollectionView 单行布局

    我正在尝试使用以下命令设置简单的水平布局UICollectionView 兜圈子却没有达到预期的结果 所以任何指针或例子将不胜感激 我粘贴经常更改的代码但没有成功可能没什么意义 该图像显示两行 第一行是单个项目 尺寸正确并且在中心正确对齐
  • UIView晃动动画

    我试图在按下按钮时使 UIView 摇动 我正在调整我找到的代码http www cimgf com 2008 02 27 core animation tutorial window shake effect http www cimgf
  • iOS - UITableViewCell 使文本加粗

    我有一个字符串 NSString userInfo James Johnson james 我想做的就是大胆James Johnson并保留 james正常字体 所以我尝试过的是使用NSAttributedString但为了完成这个过程 我

随机推荐

  • 在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