最近忙畢業(yè)的事情逗栽,到了6月底才忙完胶果,剛畢業(yè),然后又是公司推新版本漾根,很累很騷硼讽,所以到現(xiàn)在才有時間更新博客巢价,其實也是因為大部分時間拿去玩王者榮耀了,辣雞游戲固阁,大家千萬別玩??
今天講講在做公司新版本的時候壤躲,需要實現(xiàn)本地倒計時通知用戶的需求,因為之前沒有做過本地推送备燃,正好把這個分享一下碉克。
本地推送(UNUserNotificationCenter,UILocalNotification)
首先要了解一下在iOS10以上,我們使用UNUserNotificationCenter
來實現(xiàn)本地推送
在iOS8以上并齐,我們使用UILocalNotification
來實現(xiàn)本地推送
UNUserNotificationCenter
__IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
@interface UNMutableNotificationContent : UNNotificationContent
// Optional array of attachments.
@property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments __TVOS_PROHIBITED;
// The application badge number. nil means no change. 0 to hide.
// 通知對應(yīng)的app圖標(biāo)的紅點數(shù)量漏麦,nil意味著沒有改變,0就隱藏紅點
@property (NS_NONATOMIC_IOSONLY, copy, nullable) NSNumber *badge;
// The body of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
//通知的內(nèi)容體况褪,通常使用[NSString localizedUserNotificationStringForKey:arguments:NSString]傳入一個NSString字符串撕贞,從而展示通知的內(nèi)容
@property (NS_NONATOMIC_IOSONLY, copy) NSString *body __TVOS_PROHIBITED;
// The identifier for a registered UNNotificationCategory that will be used to determine the appropriate actions to display for the notification.
//和UITableViewCell類似的標(biāo)識符,可以用通過UNNotificationCategory來判斷测垛,從而對通知執(zhí)行不同操作
@property (NS_NONATOMIC_IOSONLY, copy) NSString *categoryIdentifier __TVOS_PROHIBITED;
// The launch image that will be used when the app is opened from the notification.
//通知開啟的啟動照片
@property (NS_NONATOMIC_IOSONLY, copy) NSString *launchImageName __TVOS_PROHIBITED;
// The sound that will be played for the notification.
//通知的提示聲音
@property (NS_NONATOMIC_IOSONLY, copy, nullable) UNNotificationSound *sound __TVOS_PROHIBITED;
// The subtitle of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
//通知的副標(biāo)題捏膨,也是用[NSString localizedUserNotificationStringForKey:arguments:]來初始化
@property (NS_NONATOMIC_IOSONLY, copy) NSString *subtitle __TVOS_PROHIBITED;
// The unique identifier for the thread or conversation related to this notification request. It will be used to visually group notifications together.
//對一個通知請求的標(biāo)識符,通呈澄辏可以用于管理多個通知
@property (NS_NONATOMIC_IOSONLY, copy) NSString *threadIdentifier __TVOS_PROHIBITED;
// The title of the notification. Use -[NSString localizedUserNotificationStringForKey:arguments:] to provide a string that will be localized at the time that the notification is presented.
//通知的標(biāo)題号涯,用[NSString localizedUserNotificationStringForKey:arguments:]來初始化
@property (NS_NONATOMIC_IOSONLY, copy) NSString *title __TVOS_PROHIBITED;
// Apps can set the userInfo for locally scheduled notification requests. The contents of the push payload will be set as the userInfo for remote notifications.
//通知傳遞的數(shù)據(jù),和NSNotification一樣
@property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *userInfo;
@end
UILocalNotification
@interface UILocalNotification : NSObject<NSCopying, NSCoding>
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// timer-based scheduling
// 通知開啟的時間锯七,使用NSDate來設(shè)置
@property(nullable, nonatomic,copy) NSDate *fireDate;
// the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer).
// pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock).
//對應(yīng)所在的時區(qū)链快,通常用[NSTimeZone localTimeZone]
@property(nullable, nonatomic,copy) NSTimeZone *timeZone;
//重復(fù)時間的方式
@property(nonatomic) NSCalendarUnit repeatInterval; // 0 means don't repeat
//重復(fù)時間的時間
@property(nullable, nonatomic,copy) NSCalendar *repeatCalendar;
// location-based scheduling
// set a CLRegion object to trigger the notification when the user enters or leaves a geographic region, depending upon the properties set on the CLRegion object itself. registering multiple UILocalNotifications with different regions containing the same identifier will result in undefined behavior. the number of region-triggered UILocalNotifications that may be registered at any one time is internally limited. in order to use region-triggered notifications, applications must have "when-in-use" authorization through CoreLocation. see the CoreLocation documentation for more information.
// 設(shè)置一個位置區(qū)域,當(dāng)用戶進(jìn)入或者離開一個區(qū)域的時候眉尸,會觸發(fā)推送域蜗,使用的時候記得設(shè)置關(guān)于定位的權(quán)限
@property(nullable, nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
// when YES, the notification will only fire one time. when NO, the notification will fire every time the region is entered or exited (depending upon the CLRegion object's configuration). default is YES.
// 設(shè)置通過位置觸發(fā)的通知次數(shù)是不是只有一次巨双,默認(rèn)是YES
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
// alerts
// 通知的內(nèi)容體
@property(nullable, nonatomic,copy) NSString *alertBody; // defaults to nil. pass a string or localized string key to show an alert
// 是否有按鈕,默認(rèn)是有
@property(nonatomic) BOOL hasAction; // defaults to YES. pass NO to hide launching button/slider
// 提示按鈕的文字
@property(nullable, nonatomic,copy) NSString *alertAction; // used in UIAlert button or 'slide to unlock...' slider in place of unlock
// 提示按鈕的啟動頁路徑
@property(nullable, nonatomic,copy) NSString *alertLaunchImage; // used as the launch image (UILaunchImageFile) when launch button is tapped
// 通知的標(biāo)題
@property(nullable, nonatomic,copy) NSString *alertTitle NS_AVAILABLE_IOS(8_2); // defaults to nil. pass a string or localized string key
// sound
// 通知的音頻名字
@property(nullable, nonatomic,copy) NSString *soundName; // name of resource in app's bundle to play or UILocalNotificationDefaultSoundName
// badge
// 通知的紅點
@property(nonatomic) NSInteger applicationIconBadgeNumber; // 0 means no change. defaults to 0
// user info
// 通知的數(shù)據(jù)
@property(nullable, nonatomic,copy) NSDictionary *userInfo; // throws if contains non-property list types
// category identifer of the local notification, as set on a UIUserNotificationCategory and passed to +[UIUserNotificationSettings settingsForTypes:categories:]
@property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);
@end
代碼實踐(業(yè)務(wù)場景是在開啟時間5分鐘前本地推送提示用戶)
//首先引入頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0f) {
// 使用 UNUserNotificationCenter 來管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需創(chuàng)建一個包含待通知內(nèi)容的 UNMutableNotificationContent 對象霉祸,注意不是 UNNotificationContent ,此對象為不可變對象炉峰。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"APP_NAME" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"【APP_NAME %@】 5分鐘后開始搶標(biāo)",self.model.title]
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.userInfo = @{
//對應(yīng)的數(shù)據(jù)參數(shù)
};
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:self.model.startTime];
NSTimeInterval startTimeInterval = [date timeIntervalSinceDate:[NSDate date]];
// 在發(fā)售時間的5分鐘前 推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:startTimeInterval - 5 * 60 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveMethod"
content:content
trigger:trigger];
//添加推送成功后的處理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
//推送失敗的話 提示用戶
if(error) {
//提示用戶
return ;
}
//推送成功的話 提示使用者并且更新UI
}];
} else if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f){
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 設(shè)置觸發(fā)通知的時間
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSDate *date = [dateFormatter dateFromString:self.model.startTime];
NSDate *fireDate = [NSDate dateWithTimeInterval:-5 * 60 sinceDate:date];
notification.fireDate = fireDate;
// 時區(qū)
notification.timeZone = [NSTimeZone defaultTimeZone];
// 設(shè)置重復(fù)的間隔
notification.repeatInterval = kCFCalendarUnitSecond;
// 通知內(nèi)容
notification.alertBody = [NSString stringWithFormat:@"【APP_NAME %@】 5分鐘后開始搶標(biāo)",self.model.title];
notification.alertTitle = @"APP_NAME";
notification.applicationIconBadgeNumber = 1;
// 通知被觸發(fā)時播放的聲音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知參數(shù)
notification.userInfo = @{
//對應(yīng)的數(shù)據(jù)參數(shù)
};
// ios8后脉执,需要添加這個注冊疼阔,才能得到授權(quán)
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重復(fù)提示的單位,可以是天半夷、周婆廊、月
notification.repeatInterval = 0;
}
// 執(zhí)行通知注冊
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//推送成功的話 提示用戶
});
}
//回調(diào)處理
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//iOS8版本下的處理
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
//遠(yuǎn)程通知
}
else {
// 判斷為本地通知
//iOS10下面處理
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
總結(jié)
本地通知其實和遠(yuǎn)程通知差不多,唯一要小心的就是代碼中不要加上移除本地通知的代碼巫橄,不然會導(dǎo)致本地通知的通知不正常
[[UIApplication sharedApplication] cancelAllLocalNotifications]
或者
[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification]
因為公司之前是使用極光推送的淘邻,所以上面代碼中的iOS10回調(diào)是JPush封裝好的回調(diào),如果不是使用極光推送的開發(fā)者湘换,則是在UNUserNotificationCenterDelegate的中處理回調(diào)即可
@protocol UNUserNotificationCenterDelegate <NSObject>
@optional
// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
@end