通知的原理,在這里就不做過(guò)多的解釋了,不懂的朋友可以自行Google
這里我們主要說(shuō)下本地推送。
目前來(lái)說(shuō)本地通知主要分為iOS 10 及 iOS 10以下兩種不同方式
iOS 10及其以上的寫(xiě)法
+(void) createNotification
{
//設(shè)置通知內(nèi)容
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
content.title = @"推送的標(biāo)題";
content.body = @"推送的內(nèi)容";
content.subtitle = @"推送的子標(biāo)題";
content.badge = [NSNumber numberWithInteger:0];// 紅標(biāo)
content.sound = [UNNotificationSound defaultSound];
//[UNNotificationSound soundNamed:@""]; 自定義推送聲音
//給通知添加本地圖片或者視頻,寫(xiě)法同下
NSString *path = [[NSBundle mainBundle] pathForResource:@"rank2" ofType:@"png"];
NSError *error = nil;
/*
UNNotificationAttachment是指可以包含音頻,圖像或視頻內(nèi)容。使用本地通知時(shí),可以在通知?jiǎng)?chuàng)建時(shí),將附件加入即可。對(duì)于遠(yuǎn)程通知,則必須實(shí)現(xiàn)使用UNNotificationServiceExtension類(lèi)通知服務(wù)擴(kuò)展。
*/
UNNotificationAttachment *img_attachment = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"%@", error);
}
content.attachments = @[img_attachment];//默認(rèn)只顯示第一個(gè)
content.launchImageName = @"launch2";
//消息的處理按鈕
UNTextInputNotificationAction *action1 = [UNTextInputNotificationAction actionWithIdentifier:@"replyAction" title:@"回復(fù)" options:UNNotificationActionOptionNone];
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"enterAction" title:@"進(jìn)入應(yīng)用" options:UNNotificationActionOptionForeground];
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"cancelAction" title:@"取消" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"CategroyAction" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
content.categoryIdentifier = @"CategroyAction";
//設(shè)置推送的觸發(fā)機(jī)制
UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
//日歷形式,
// NSDateComponents * dateComp = [[NSDateComponents alloc] init];
// dateComp.weekday = 2;//周一,默認(rèn)第一天是周日
// dateComp.month = 12;//月份,您可以在這里設(shè)置具體的時(shí)分秒
// dateComp.hour = 10;//小時(shí)
// dateComp.minute = 14;//分鐘
// UNCalendarNotificationTrigger * calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComp repeats:YES];
//地理位置推送.沒(méi)實(shí)際應(yīng)用過(guò)
// CLLocationCoordinate2D location ;
// location.latitude = 123;
// location.longitude = 111;
//
// CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:location radius:100.0 identifier:@"regionid"];
//
// UNLocationNotificationTrigger * locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:YES];
NSString * identifier = @"notification_onlytextid";
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
}
在iOS 10上 我們可以定義對(duì)于推送的各種快捷操作,那么我們的如何實(shí)現(xiàn)操作方法呢?
答案就是代理,蘋(píng)果官方的API幾乎都是如此!
首先 遵循 代理 <UNUserNotificationCenterDelegate>
實(shí)現(xiàn)代理方法,在實(shí)際開(kāi)發(fā)中,注意對(duì)應(yīng)的 categoryid
-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSString * categoryid = response.notification.request.content.categoryIdentifier;
NSLog(@"收到通知:%@",response.notification.request.content);
//識(shí)別同類(lèi)類(lèi)型id
if ([categoryid isEqualToString:@"CategroyAction"])
{
//識(shí)別通知操作動(dòng)作的id
if ([response.actionIdentifier isEqualToString:@"replyAction"])
{
UNTextInputNotificationResponse * textResponse = (UNTextInputNotificationResponse *) response;
NSString * text = textResponse.userText;
//此為輸入的內(nèi)容
}
}
completionHandler();
}
iOS 10上 還有很多操作性的API,修改通知的內(nèi)容或移除通知等
- identifiers 為我們?yōu)楸镜赝ㄖO(shè)置的id
//獲取所有的未發(fā)出的通知,
- (void)getPendingNotificationRequestsWithCompletionHandler:(void(^)(NSArray<UNNotificationRequest *> *requests))completionHandler;
// 移除 identifiers 里面的通知
- (void)removePendingNotificationRequestsWithIdentifiers:(NSArray<NSString *> *)identifiers;
//獲取發(fā)出的通知
- (void)getDeliveredNotificationsWithCompletionHandler:(void(^)(NSArray<UNNotification *> *notifications))completionHandler __TVOS_PROHIBITED;
// 移除 identifiers 里面 所有的 發(fā)出的通知
- (void)removeDeliveredNotificationsWithIdentifiers:(NSArray<NSString *> *)identifiers __TVOS_PROHIBITED;
- (void)removeAllDeliveredNotifications __TVOS_PROHIBITED;
iOS 10以下
UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.alertTitle = @"推送的標(biāo)題";
notification.alertBody = @"推送的內(nèi)容";
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];//10s 后
notification.soundName = UILocalNotificationDefaultSoundName;
// [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 立即觸發(fā)
[[UIApplication sharedApplication] scheduleLocalNotification:notification];//到設(shè)定時(shí)間觸發(fā)
上篇:iOS推送權(quán)限開(kāi)發(fā)判斷
下篇:iOS通知消息的處理
如有瑕疵之處,望大家不吝指教