簡述
UILocalNotification
一個(gè)UILocalNotification對象指定一個(gè)通知疤祭,一個(gè)應(yīng)用程序可以在特定的日期和時(shí)間安排進(jìn)行演示狼纬。操作系統(tǒng)負(fù)責(zé)在其預(yù)定時(shí)間提供本地通知;要做到這一點(diǎn)可以應(yīng)用程序不必運(yùn)行顿痪。雖然和通知類似,因?yàn)樗鼈冇糜陲@示警報(bào)主穗,播放聲音和徽章應(yīng)用程序圖標(biāo)遠(yuǎn)程通知悲关,它們是由與本地傳遞,并且不需要與遠(yuǎn)程服務(wù)器連接撤逢。
本地通知主要用于應(yīng)用程序與基于計(jì)時(shí)器的行為和簡單的日歷或待辦事項(xiàng)應(yīng)用程序膛锭。即在后臺運(yùn)行的應(yīng)用程序還可以安排本地通知粮坞,告知進(jìn)入的消息,聊天初狰,或更新的用戶莫杈。一個(gè)應(yīng)用程序只能有計(jì)劃通知的數(shù)量有限;
當(dāng)您創(chuàng)建一個(gè)本地通知,您必須指定一個(gè)特定日期或地理區(qū)域?yàn)橛|發(fā)交付通知跷究⌒昭福基于日期的通知在指定的日期和時(shí)間交付,可時(shí)區(qū)變化需要作出俊马。當(dāng)用戶進(jìn)入或退出特定區(qū)域的基于區(qū)域的通知交付丁存。在這兩種情況下,你可以指定通知書是否是一次性事件或可再次改期并交付使用柴我。
或presentLocalNotificationNow:UiApplication類的方法創(chuàng)建一個(gè)UILocalNotification對象后解寝,即使用scheduleLocalNotification安排吧。該scheduleLocalNotification:方法使用火日期如期交貨;該presentLocalNotificationNow:方法立即呈現(xiàn)通知艘儒,而不管fireDate的值聋伦。您可以取消使用cancelLocalNotification一個(gè)或多個(gè)本地通知:或cancelAllLocalNotifications的對象的UIApplication方法。
當(dāng)系統(tǒng)提供了一個(gè)本地通知界睁,幾件事情可能發(fā)生觉增,這取決于該應(yīng)用狀態(tài)和通知類型。如果應(yīng)用程序是不是最前面可見翻斟,系統(tǒng)會顯示警告信息逾礁,徽章的應(yīng)用程序,播放聲音访惜,無論是在通知規(guī)定的嘹履。如果通知是警報(bào)和用戶點(diǎn)擊操作按鈕(或者,如果設(shè)備被鎖定债热,拖動打開動作滑塊)砾嫉,應(yīng)用程序被喚醒或啟動。 (如果用戶點(diǎn)擊指定使用additionalActions屬性自定義操作之一窒篱,應(yīng)用程序被喚醒或射入背景)焕刮。在其應(yīng)用:didFinishLaunchingWithOptions:方法中,應(yīng)用程序的委托可以從啟動選項(xiàng)字典獲得UILocalNotification對象使用UIApplicationLaunchOptionsLocalNotificationKey鍵墙杯。委托可以檢查該通知的屬性济锄,并且如果通知包括在其用戶信息的字典的自定義數(shù)據(jù),它可以訪問該數(shù)據(jù)霍转,并相應(yīng)地進(jìn)行處理。另一方面一汽,如果本地通知只徽章的應(yīng)用圖標(biāo)避消,并且響應(yīng)用戶啟動該應(yīng)用程序低滩,該應(yīng)用程序:didFinishLaunchingWithOptions:方法中被調(diào)用,但不UILocalNotification對象被包括在選項(xiàng)字典岩喷。當(dāng)用戶選擇一個(gè)自定義操作恕沫,應(yīng)用程序委托的申請:handleActionWithIdentifier:forLocalNotification:completionHandler:方法被調(diào)用來處理的動作。
如果應(yīng)用程序是最重要的可見當(dāng)系統(tǒng)提供的通知后纱意,應(yīng)用程序委托的申請:didReceiveLocalNotification:被調(diào)用來處理通知婶溯。使用所提供的UILocalNotification對象中的信息來決定采取何種行動。該系統(tǒng)不顯示任何警報(bào)偷霉,徽章應(yīng)用程序的圖標(biāo)迄委,或者發(fā)出任何聲音時(shí),應(yīng)用程序已經(jīng)最前面类少。
一個(gè)應(yīng)用程序是負(fù)責(zé)管理其圖標(biāo)顯示的證件號碼叙身。例如,如果一個(gè)短信應(yīng)用接收本地通知后硫狞,處理所有傳入的消息信轿,應(yīng)該由的UIApplication對象的applicationIconBadgeNumber屬性設(shè)置為0刪除圖標(biāo)徽章。
效果展示
代碼
發(fā)送代碼
- (IBAction)SendNotification_Click:(id)sender {
//新建本地通知
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//觸發(fā)通知時(shí)間
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:6];
//重復(fù)間隔
//localNotification.repeatInterval = kCFCalendarUnitDay;
//設(shè)置通知的交付日期
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//通知內(nèi)容
localNotification.alertBody = @"本地通知";
//應(yīng)用角標(biāo)
localNotification.applicationIconBadgeNumber = 1;
//應(yīng)用提示音
localNotification.soundName = UILocalNotificationDefaultSoundName;
//通知參數(shù)
localNotification.userInfo = @{@"key": @"残吩。财忽。。泣侮。"};
//這組事件的名稱
localNotification.category = @"tblocalNotification";
//發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
可以設(shè)置系統(tǒng)提示方式
如果設(shè)置為提醒模式即彪,那么可在SendNotification_Click
方法里追加
//提示框的按鈕,提示方式要設(shè)置為alert
localNotification.alertAction = @"查看"; //提示框按鈕
localNotification.hasAction = YES; //是否顯示額外的按鈕旁瘫,為no時(shí)alertAction消失
如果設(shè)置為橫幅模式
UIMutableUserNotificationAction
對象,當(dāng)一個(gè)通知交付祖凫,系統(tǒng)會顯示與通知相關(guān)聯(lián)的每個(gè)自定義操作的按鈕。按一個(gè)按鈕就啟動您的應(yīng)用(無論是在前臺或后臺)酬凳,并為您進(jìn)行相應(yīng)的操作機(jī)會惠况。您可以使用這個(gè)類來配置有關(guān),顯示該按鈕的細(xì)節(jié)和信息您的應(yīng)用程序需要執(zhí)行相應(yīng)的動作宁仔。
- (void)registerLocalNotification{
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
//內(nèi)部識別操作的字符串
action1.identifier = @"smile";
//本地化的字符串作為按鈕標(biāo)題為操作使用
action1.title = @"";
//在該模式下進(jìn)行操作時(shí)運(yùn)行應(yīng)用程序(在后臺處理)
action1.activationMode = UIUserNotificationActivationModeBackground;
//指示執(zhí)行操作之前稠屠,用戶是否必須解鎖設(shè)備。
action1.authenticationRequired = YES;
//destructive 破壞性按鈕翎苫,多用于刪除
//destructive屬性設(shè)置后权埠,在通知欄或鎖屏界面左劃,按鈕顏色會變?yōu)榧t色
//如果兩個(gè)按鈕均設(shè)置為YES煎谍,則均為紅色(略難看)
//如果兩個(gè)按鈕均設(shè)置為NO攘蔽,即默認(rèn)值,則第一個(gè)為藍(lán)色呐粘,第二個(gè)為淺灰色
//如果一個(gè)YES一個(gè)NO满俗,則都顯示對應(yīng)的顏色转捕,即紅藍(lán)雙色 (CP色)。
action1.destructive = NO;
//第二個(gè)動作
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"text";
action2.title = @"評論";
//當(dāng)點(diǎn)擊的時(shí)候不啟動程序唆垃,在后臺處理
action2.activationMode = UIUserNotificationActivationModeBackground;
//設(shè)置了behavior屬性為 UIUserNotificationActionBehaviorTextInput 的話五芝,則用戶點(diǎn)擊了該按鈕會出現(xiàn)輸入框供用戶輸入
action2.behavior = UIUserNotificationActionBehaviorTextInput;
//這個(gè)字典定義了當(dāng)用戶點(diǎn)擊了右按鈕后,輸入框右側(cè)的按鈕名稱辕万,如果不設(shè)置該字典枢步,則右側(cè)按鈕名稱默認(rèn)為 “發(fā)送”
action2.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"發(fā)送"};
//創(chuàng)建動作(按鈕)的類別集合
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
//這組動作的唯一標(biāo)示(要和你發(fā)送通知時(shí)填寫的cateory的名稱保持一致)
category.identifier = @"tblocalNotification";
//最多支持兩個(gè),如果添加更多的話渐尿,后面的將被忽略
[category setActions:@[action1, action2] forContext:(UIUserNotificationActionContextDefault)];
//創(chuàng)建UIUserNotificationSettings醉途,并設(shè)置消息的顯示類類型
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObject:category]];
//注冊通知
[[UIApplication sharedApplication] registerUserNotificationSettings:uns];
}
//注冊服務(wù)器推送,在應(yīng)用第一次啟動時(shí)彈出對話框讓用戶確認(rèn)是否開啟消息推送涡戳,注冊的消息類型有BadgeNumber, 聲音, 頂部消息框. 可以選擇其中的一種或多種结蟋。
// [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
// (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
上面代碼最好在應(yīng)用第一次啟動時(shí)注冊通知,即 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法內(nèi)
應(yīng)用程序正在運(yùn)行接收到本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@", notification.userInfo);
NSLog(@"app在前臺狀態(tài)時(shí)收到推送消息");
}
在非本App界面時(shí)收到本地消息渔彰,下拉消息會有快捷回復(fù)的按鈕嵌屎,點(diǎn)擊按鈕后調(diào)用的方法,根據(jù)identifier來判斷點(diǎn)擊的哪個(gè)按鈕恍涂,notification為消息內(nèi)容
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler
{
if ([identifier isEqualToString:@"smile"]) {
NSLog(@"smile");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"smile" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
} else if ([identifier isEqualToString:@"text"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"評論:%@", responseInfo[UIUserNotificationActionResponseTypedTextKey]] message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
}
completionHandler();
}
博主在測試的時(shí)候宝惰,設(shè)置了循環(huán)通知,然后就是被一直提醒再沧,把代碼循環(huán)通知注釋掉也沒有用尼夺,最后,想到了炒瘸,是不是有注銷通知的方法淤堵,經(jīng)查:
- (IBAction)remove:(id)sender {
for (UILocalNotification *obj in [UIApplication sharedApplication].scheduledLocalNotifications) {
if ([obj.userInfo.allKeys containsObject:@"key"]) {
[[UIApplication sharedApplication] cancelLocalNotification:obj];
}
}
//直接取消全部本地通知
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
最后,要在開啟
APNS
注冊通知
在應(yīng)用第一次啟動時(shí)注冊通知顷扩,即 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法內(nèi)
//注冊服務(wù)器推送拐邪,在應(yīng)用第一次啟動時(shí)彈出對話框讓用戶確認(rèn)是否開啟消息推送,本句注冊的消息類型有BadgeNumber, 聲音, 頂部消息框. 可以選擇其中的一種或多種隘截。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
獲取token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
//獲取終端設(shè)備標(biāo)識扎阶,這個(gè)標(biāo)識需要通過接口發(fā)送到服務(wù)器端,服務(wù)器端推送消息到APNS時(shí)需要知道終端的標(biāo)識婶芭,APNS通過注冊的終端標(biāo)識找到終端設(shè)備东臀。
NSLog(@"My token is:%@", token);
}
獲取消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
{
//在此處理接收到的消息。
NSLog(@"Receive remote notification : %@",userInfo);
}
本文到這里就結(jié)束了犀农,APNS可以結(jié)合本地通知進(jìn)行使用惰赋!