一、开始的启蒙
开始想到做这个功能,也是在早上坐公交车上班的时候翻阅技术公众号看到的,抽空就自己实现了一下。
二、开发主要步骤
- 准备好更换的图标
-
修改plist文件,放好图标的位置
1.CFBundleIcons:一个字典,包含所有AppIcon信息,即上图的Icon files(iOS 5)
2.CFBundlePrimaryIcon:如果已经在Assets.xcassets中设置了AppIcon,那么CFBundlePrimaryIcon中的配置会被忽略,3.Assets.xcassets的AppIcon将会自动配置到CFBundlePrimaryIcon中。
4.CFBundleAlternateIcons:一个数组,负责配置可供替换的icon信息
5.UIPrerenderedIcon:是否已经预渲染,如果不设置该项或者设为NO。系统会自动为icon进行渲染增加光泽 - 编写代码
1.引入工具类:FSAppIconManager
2.添加更换按钮
3.修改AppDelegate.m文件
///添加本地通知
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 注册通知
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(!error)
{
NSLog(@"@授权成功");
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
} else {
// Fallback on earlier versions
}
return YES;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
NSLog(@"willPresentNotification:%@",notification.request.content.title);
// 这里真实需要处理交互的地方
// 获取通知所带的数据
NSString *apsContent = [notification.request.content.userInfo objectForKey:@"type"];
NSLog(@"%@",apsContent);
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
//在没有启动本App(包括后台运行中)时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
NSString *apsContent = [response.notification.request.content.userInfo objectForKey:@"type"];
NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);
NSLog(@"%@",apsContent);
}
4、在viewController中编写点击事件
#import "FSAppIconManager.h"
#import <UserNotifications/UserNotifications.h>
@interface ViewController () <UNUserNotificationCenterDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *name = [FSAppIconManager getCurrentAppIconName];
NSLog(@"name: %@", name);
}
- (IBAction)changeAppIcon:(id)sender {
BOOL canChangeAppIcon = [FSAppIconManager canChangeAppIcon];
NSLog(@"canChangeAppIcon value: %@", canChangeAppIcon?@"YES":@"NO");
if (!canChangeAppIcon) {
return;
}
NSString *name = [FSAppIconManager getCurrentAppIconName];
NSString *changeName = @"male";
if ([name isEqualToString:@"male"]) {
changeName = @"female";
}
[FSAppIconManager changeAppIconWithIconName:changeName completionHandler:^(NSError * _Nullable error) {
NSLog(@"error: %@", error);
}];
}
- (IBAction)sendLocalPush:(UIButton *)sender {
//8s后提醒
// UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:8 repeats:NO];
//每周一早上 8:00 提醒我给起床
// NSDateComponents *components = [[NSDateComponents alloc] init];
// components.weekday = 2;
// components.hour = 8;
// UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
//
// //需导入定位库 #import <CoreLocation/CoreLocation.h>
// //一到距离(123.333, 123.344)点20米就喊我下车
// CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:CLLocationCoordinate2DMake(123.333, 123.344) radius:20 identifier:@"regionidentifier"];
//
// UNLocationNotificationTrigger *trigger4 = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];
if (@available(iOS 10.0, *)) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"this is Notifications";
content.subtitle = @"本地通知";
content.body = @"推送一条本地通知";
content.badge = @1;
content.userInfo = @{@"type":@"this is a userNotification"};
//每小时重复 1 次喊我喝水
UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:trigger2];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"本地消息推送失败:%@", error);
}
}];
} else {
// Fallback on earlier versions
}
}
网友评论