{"id":20073324,"url":"https://github.com/1691665955/mznotification","last_synced_at":"2026-01-07T16:01:40.052Z","repository":{"id":152808996,"uuid":"188153884","full_name":"1691665955/MZNotification","owner":"1691665955","description":"the demo of local notification and simple module","archived":false,"fork":false,"pushed_at":"2022-04-08T06:18:27.000Z","size":451,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T01:33:51.299Z","etag":null,"topics":["alarm-clock","ios","notification","nsnotification","objective-c"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1691665955.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-23T03:19:43.000Z","updated_at":"2023-08-07T01:34:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"25336f80-9508-4250-96ea-91cc92e82f3a","html_url":"https://github.com/1691665955/MZNotification","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1691665955%2FMZNotification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1691665955%2FMZNotification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1691665955%2FMZNotification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1691665955%2FMZNotification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1691665955","download_url":"https://codeload.github.com/1691665955/MZNotification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245914499,"owners_count":20693184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alarm-clock","ios","notification","nsnotification","objective-c"],"created_at":"2024-11-13T14:44:53.932Z","updated_at":"2026-01-07T16:01:35.009Z","avatar_url":"https://github.com/1691665955.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MZNotification\n## 前言\n最近开发一个项目是关于门禁系统的，会有门口机呼叫app的功能。本项目运用了voip来进行推送并唤醒app，然后利用本地推送来进行弹框提醒，目前该项目也已基本完成。联想到本人之前也做过类似闹钟功能的项目也用到了本地推送，所以想到对本地推送和远程推送进行简单的封装，待下次使用的时候能够更方便一点。\n\n### 本地推送\n#### UILocalNotification（iOS8-iOS10）\n在iOS10之前我们是使用UILocalNotification来进行本地推送的，接下来我们来介绍下简单的流程。\n- 注册本地通知\n```\nUIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];\n[[UIApplication sharedApplication] registerUserNotificationSettings:settings];\n```\n- 创建通知\n```\nUILocalNotification *notification = [[UILocalNotification alloc] init];\nif (@available(iOS 8.2, *)) {\nnotification.alertTitle = title;//设置推送的标题\n}\nnotification.alertBody = message;//设置推送的内容\nnotification.soundName = UILocalNotificationDefaultSoundName;//设置推送的声音，可设置自定义铃声，但是不可超过30s\nnotification.userInfo = @{@\"tag\":@\"zzzz\"};//设置本地推送附带信息\nnotification.applicationIconBadgeNumber = badge;//设置app角标\nnotification.repeatInterval = repeatInterval;//推送重复间隔，0代表不重复\nif (fireDate) {\nnotification.fireDate = fireDate;//设置fireDate可设置推送时间\n[[UIApplication sharedApplication] scheduleLocalNotification:notification];\n} else {\n[[UIApplication sharedApplication] presentLocalNotificationNow:notification];//立即执行推送\n}\n```\n- 删除通知\n1. 删除指定通知，在notification的userInfo中存储指定的标志符，但删除的时候在找出对应的标志符的notification来删除。\n```\nNSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];//获取所有本地推送\nfor (UILocalNotification *notification in localNotifications) {\nNSDictionary *info = notification.userInfo;\nif (info) {\nNSString *currentIdentifier = [info valueForKey:@\"MZNotification_identifier\"];\nif ([identifier isEqualToString:currentIdentifier]) {\n[[UIApplication sharedApplication] cancelLocalNotification:notification];\nbreak;\n}\n}\n}\n```\n2. 删除所有通知\n```\n[[UIApplication sharedApplication] cancelAllLocalNotifications];\n```\n- 更新通知\n先找到要更新的通知，删除该通知，再重新创建一个新的通知，具体操作步骤请参考上面的删除通知和创建通知。\n\n- 通知代理回调\n当app处于前台时收到推送信息时或app处于后台时点击推送时\n```\n- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {\nNSDictionary *userInfo = notification.userInfo;\nif ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {\n//app处于前台时收到推送信息时\n} else {\n//app处于后台时点击推送时\n}\n}\n```\n\n#### UNUserNotificationCenter（iOS10之后）\n在iOS10之后我们使用UNUserNotificationCenter来创建和管理本地通知，接下来我们同样介绍下简单的流程。\n- 注册本地通知\n```\nUNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];\n[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {\nif (!error) {\nNSLog(@\"request authorization succeeded!\");\n}\n}];\ncenter.delegate = self;\n```\n- 创建通知\n```\nUNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];//通知管理中心\nUNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];//创建推送内容对象\ncontent.title = title;//推送标题\ncontent.body = message;//推送内容\ncontent.sound = UNNotificationSound.defaultSound;//设置推送铃声\n//content.sound = [UNNotificationSound soundNamed:sound];//设置自定义铃声，sound为铃声名称\ncontent.userInfo = @{@\"tag\":@\"zzzz\"};//设置本地推送附带信息\ncontent.badge = [NSNumber numberWithInteger:badge];//设置app角标\n//初始化本地通知请求，具体参数这里就不多赘述了，大家可以自行baidu\nUNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier\ncontent:content trigger:trigger];\n[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {\n\n}];\n```\n- 删除通知\n1. 删除指定通知,这里只要知道通知的identifier，就可以删除指定的通知\n```\n[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]];\n```\n2. 删除所有通知\n```\n[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];\n```\n- 更新通知\niOS10之后的更新通知比UILocalNotification简单些，只需要重新创建一个相同identifier的本地通知即可，此时新的通知会自动替换掉相同identifier的通知，具体的创建通知的操作请参考上面的创建通知。\n\n- 通知代理回调\napp处于前台时\n```\n- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)){\ncompletionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);\n}\n```\napp点击推送消息时\n```\n- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)) {\ncompletionHandler();\n}\n```\n\n### 远程推送\n\n- 远程推送的注册和本地通知的基本一样，最后多加一个一行代码去请求deviceToken即可\n```\n[[UIApplication sharedApplication] registerForRemoteNotifications];\n```\n注册完远程通知后获取到deviceToken，将deviceToken通过接口传给后台，后台通过deviceToken就可以推送通知到指定设备了。\n\n- 远程推送的消息代理回调基本和本地推送的一样，只是iOS8-iOS10的推送到达的代理回调变为\n```\n- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {\ncompletionHandler(UIBackgroundFetchResultNoData);\n}\n```\n\n## 针对本地通知和远程推送的功能进行简单的封装\n- 本地通知\n```\n@protocol MZLocalNotificationDelegate \u003cNSObject\u003e\n\n@optional\n\n/**\napp处于前台时收到推送数据\n\n@param userInfo 推送数据\n*/\n- (void)mz_didReceiveLocalNotificationOnApplicationActiveWithUserInfo:(NSDictionary *)userInfo;\n\n/**\napp处于后台时收到推送，点击推送后会调用该代理\n\n@param userInfo 推送数据\n*/\n- (void)mz_didReceiveLocalNotificationOnApplicationBackgroundWithUserInfo:(NSDictionary *)userInfo;\n\n@end\n\n@interface MZLocalNotification : NSObject \u003cUIApplicationDelegate, UNUserNotificationCenterDelegate\u003e\n\n/**\nMZLocalNotification推送数据回调代理\n*/\n@property (nonatomic, weak) id\u003cMZLocalNotificationDelegate\u003e delegate;\n\n/**\n当app处于前台时是否弹出推送框，默认弹出（此方法只对iOS10以后的系统有效，因为iOS10之前的app处于前台时，app是不能弹出推送弹框的）\n*/\n@property (nonatomic, assign) BOOL showNotificationWhenApplicationActice;\n\n+ (instancetype)shareInstance;\n\n/**\n注册本地推送，获取授权\n*/\n- (void)registerLocalNotification;\n\n/**\niOS8-iOS10发送本地推送\n\n@param badge 角标\n@param sound 推送声音（nil代表系统默认声音，可填写自定义推送声音名称）\n@param title 推送标题\n@param message 推送内容\n@param params 推送额外附带数据，会在推送数据回调代理中获取到\n@param fireDate 触发时间（nil代表立即触发）\n@param repeatInterval 重复时间间隔，0代表不重复\n@param identifier 通知标志符，可用来更新和删除本地通知\n*/\n- (void)pushLocalNotificationWithBadge:(NSInteger)badge sound:(nullable NSString *)sound title:(NSString *)title message:(NSString *)message params:(NSDictionary *)params fireDate:(nullable NSDate *)fireDate repeatInterval:(NSCalendarUnit)repeatInterval identifier:(nonnull NSString *)identifier;\n\n/**\niOS10之后发送本地推送\n\n@param badge 角标\n@param sound 推送声音（nil代表系统默认声音，可填写自定义推送声音名称）\n@param title 推送标题\n@param message 推送内容\n@param params 推送额外附带数据，会在推送数据回调代理中获取到\n@param trigger 推送触发器\n@param identifier 通知标志符，可用来更新和删除本地通知\n*/\n- (void)pushLocalNotificationWithBadge:(NSInteger)badge sound:(nullable NSString *)sound title:(NSString *)title message:(NSString *)message params:(NSDictionary *)params trigger:(nullable UNNotificationTrigger *)trigger identifier:(nonnull NSString *)identifier API_AVAILABLE(ios(10.0));\n\n/**\niOS8-iOS10更新本地推送(相同identifier的推送会替换)\n\n@param badge 角标\n@param sound 推送声音（nil代表系统默认声音，可填写自定义推送声音名称）\n@param title 推送标题\n@param message 推送内容\n@param params 推送额外附带数据，会在推送数据回调代理中获取到\n@param fireDate 触发时间（nil代表立即触发）\n@param repeatInterval 重复时间间隔，0代表不重复\n@param identifier 通知标志符，可用来更新和删除本地通知\n*/\n- (void)updateLocalNotificationWithBadge:(NSInteger)badge sound:(nullable NSString *)sound title:(NSString *)title message:(NSString *)message params:(NSDictionary *)params fireDate:(nullable NSDate *)fireDate repeatInterval:(NSCalendarUnit)repeatInterval identifier:(nonnull NSString *)identifier;\n\n/**\niOS10之后更新本地推送(相同identifier的推送会替换)\n\n@param badge 角标\n@param sound 推送声音（nil代表系统默认声音，可填写自定义推送声音名称）\n@param title 推送标题\n@param message 推送内容\n@param params 推送额外附带数据，会在推送数据回调代理中获取到\n@param trigger 推送触发器\n@param identifier 通知标志符，可用来更新和删除本地通知\n*/\n- (void)updateLocalNotificationWithBadge:(NSInteger)badge sound:(nullable NSString *)sound title:(NSString *)title message:(NSString *)message params:(NSDictionary *)params trigger:(nullable UNNotificationTrigger *)trigger identifier:(nonnull NSString *)identifier API_AVAILABLE(ios(10.0));\n\n/**\n取消指定标志符的本地推送\n\n@param identifier 通知标志符\n*/\n- (void)cancelLocalNotificationWithIdentifier:(nonnull NSString *)identifier;\n\n/**\n取消所有本地推送\n*/\n- (void)cancelAllLocalNotification;\n\n/**\n设置app角标数量\n\n@param badge 角标数量\n*/\n- (void)setApplicationIconBadgeNumber:(NSInteger)badge;\n\n/**\n去除app角标\n*/\n- (void)clearApplicationIconBadge;\n\n@end\n```\n\n- 远程推送\n```\n@protocol MZRemoteNotificationDelegate \u003cNSObject\u003e\n\n@optional\n\n/**\n远程推送获取deviceToken\n\n@param token NSData类型deviceToken\n@param tokenString NSString类型deviceToken\n*/\n- (void)mz_didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token tokenString:(NSString *)tokenString;\n\n/**\napp处于前台时收到推送数据\n\n@param userInfo 推送数据\n*/\n- (void)mz_didReceiveRemoteNotificationOnApplicationActiveWithUserInfo:(NSDictionary *)userInfo;\n\n/**\napp处于后台时收到推送，点击推送后会调用该代理\n\n@param userInfo 推送数据\n*/\n- (void)mz_didReceiveRemoteNotificationOnApplicationBackgroundWithUserInfo:(NSDictionary *)userInfo;\n@end\n\n\n@interface MZRemoteNotification : NSObject \u003cUIApplicationDelegate\u003e\n/**\nMZRemoteNotification推送数据回调代理\n*/\n@property (nonatomic, weak) id\u003cMZRemoteNotificationDelegate\u003e delegate;\n\n/**\n当app处于前台时是否弹出推送框，默认弹出（此方法只对iOS10以后的系统有效，因为iOS10之前的app处于前台时，app是不能弹出推送弹框的）\n*/\n@property (nonatomic, assign) BOOL showNotificationWhenApplicationActice;\n\n+ (instancetype)shareInstance;\n\n/**\n注册远程推送，获取授权\n*/\n- (void)registerRemoteNotification;\n\n/**\n设置app角标数量\n\n@param badge 角标数量\n*/\n- (void)setApplicationIconBadgeNumber:(NSInteger)badge;\n\n/**\n去除app角标\n*/\n- (void)clearApplicationIconBadge;\n\n@end\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1691665955%2Fmznotification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1691665955%2Fmznotification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1691665955%2Fmznotification/lists"}