1.delegate 注冊(cè)本地通知
#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//請(qǐng)求獲取通知權(quán)限(角標(biāo)吵护,聲音,彈框)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//獲取用戶(hù)是否同意開(kāi)啟通知
NSLog(@"request authorization successed!");
}
}];
return YES;
}
2.創(chuàng)建通知
#import <UserNotifications/UserNotifications.h>
//第二步:新建通知內(nèi)容對(duì)象
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"iOS10通知";
content.subtitle = @"新通知學(xué)習(xí)筆記";
content.body = @"新通知變化很大南捂,之前本地通知和遠(yuǎn)程推送是兩個(gè)類(lèi)法褥,現(xiàn)在合成一個(gè)了检吆。這是一條測(cè)試通知,";
content.badge = @1;
UNNotificationSound *sound = [UNNotificationSound soundNamed:@"in.caf"];
content.sound = sound;
//第三步:通知觸發(fā)機(jī)制。(重復(fù)提醒尔当,時(shí)間間隔要大于60s)
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
//第四步:創(chuàng)建UNNotificationRequest通知請(qǐng)求對(duì)象
NSString *requertIdentifier = @"RequestIdentifier";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];
//第五步:將通知加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);
}];
參考:http://www.reibang.com/p/5713fa2bfece
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者