本地推送

1、iOS8引入了地點(diǎn)通知(location notifications)。它其實(shí)也是本地通知(local notifications),但是他們只會(huì)在用戶一個(gè)特定的地理或者iBeacon區(qū)域時(shí),才會(huì)被觸發(fā)。雖然我們看不到什么細(xì)節(jié),地點(diǎn)通知 (location notifications)實(shí)現(xiàn)起來(lái)也很容易。

2、從iOS8開(kāi)始,通知被加入了新的特性。簡(jiǎn)單地說(shuō),從現(xiàn)在開(kāi)始,當(dāng)一個(gè)通知被展示時(shí),開(kāi)發(fā)者可以指定用戶可觸發(fā)的具體的動(dòng)作(actions),而且甚至不用啟動(dòng)App也可以處理這個(gè)通知。動(dòng)作(Actions)可以被歸類到一個(gè)類目(categories)下。特別是當(dāng)App安排了不少通知顯示的時(shí)候相當(dāng)方便。用類目 (categories),一個(gè)通知所有相關(guān)的動(dòng)作(actions)都可以一次性的被捆綁和指定。反之,處理動(dòng)作(actions)也非常簡(jiǎn)單,只需要實(shí)現(xiàn)其代理方法即可。每一個(gè)動(dòng)作(actions)都有一個(gè)特殊的屬性標(biāo)示符(identifier),被App用來(lái)辨別收到的動(dòng)作(actions)然后適當(dāng)?shù)靥幚硭?/p>

3、可以被安排的本地通知的數(shù)量并不是無(wú)限的,最多有64個(gè)本地通知可以被安排和展示。如果多余這個(gè)數(shù)字,所有超過(guò)這個(gè)數(shù)字的通知都會(huì)被廢棄。盡管如此,無(wú)論通知是以什么樣的形式被安排的,最早的那個(gè)會(huì)被最先展示。

4、具體代碼:

在ViewController.m中添加代碼:

//創(chuàng)建通知

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

//滑動(dòng)提醒

localNotification.alertAction = @"查看消息";

localNotification.hasAction = YES;

//提醒內(nèi)容

localNotification.alertBody = @"推送消息";

//category的identifier

localNotification.category = @"COMPLETE_CATEGORY";

//推送聲音

localNotification.soundName = nil;

//觸發(fā)時(shí)間

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];

//應(yīng)用程序右上角圖標(biāo)標(biāo)記

localNotification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在AppDelegate.m中添加代碼:

在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加

UIMutableUserNotificationAction *notificationActionOk = [[UIMutableUserNotificationAction alloc]init];

notificationActionOk.identifier = @"completeRemindRater";

notificationActionOk.title = @"再工作一會(huì)兒";

//是否取消提醒

notificationActionOk.destructive = NO;

//是否需要權(quán)限,例如鎖屏的時(shí)候,執(zhí)行操作是否需要解鎖再執(zhí)行

notificationActionOk.authenticationRequired = NO;

//啟動(dòng)app還是后臺(tái)執(zhí)行

notificationActionOk.activationMode = UIUserNotificationActivationModeBackground;

notificationActionOk.behavior = UIUserNotificationActionBehaviorDefault;

UIMutableUserNotificationAction *notificationActionRestNow = [[UIMutableUserNotificationAction alloc]init];

notificationActionRestNow.identifier = @"relaxNow";

notificationActionRestNow.title = @"休息";

//是否取消提醒? 當(dāng)設(shè)置為yes時(shí),通知中相應(yīng)地按鈕的背景色會(huì)變成紅色

notificationActionRestNow.destructive = YES;

//是否需要權(quán)限,例如鎖屏的時(shí)候,執(zhí)行操作是否需要解鎖再執(zhí)行

notificationActionRestNow.authenticationRequired = NO;

//啟動(dòng)app還是后臺(tái)執(zhí)行

notificationActionRestNow.activationMode = UIUserNotificationActivationModeBackground;

notificationActionRestNow.behavior = UIUserNotificationActionBehaviorDefault;

UIMutableUserNotificationCategory *notificationCompleteCategory = [[UIMutableUserNotificationCategory alloc] init];

notificationCompleteCategory.identifier = @"COMPLETE_CATEGORY";

[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextMinimal];

[notificationCompleteCategory setActions:@[notificationActionOk,notificationActionRestNow] forContext:UIUserNotificationActionContextDefault];

UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];

replyAction.title = @"回復(fù)";

replyAction.identifier = @"inline-reply";

replyAction.activationMode = UIUserNotificationActivationModeBackground;

replyAction.authenticationRequired = NO;

replyAction.behavior = UIUserNotificationActionBehaviorTextInput;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];

notificationCategory.identifier = @"REPLY_CATEGORY";

[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextMinimal];

[notificationCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge categories:[NSSet setWithArray:@[notificationCompleteCategory,notificationCategory]]]];

實(shí)現(xiàn)協(xié)議方法:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{

if ([identifier isEqualToString:@"completeRemindRater"]) {

NSLog(@"點(diǎn)擊的是%@",identifier);

}

if ([identifier isEqualToString:@"relaxNow"]) {

NSLog(@"點(diǎn)擊的是%@",identifier);

}

if ([identifier isEqualToString:@"inline-reply"]) {

NSLog(@"點(diǎn)擊的是%@",identifier);

}

completionHandler();

}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

NSLog(@"接收到通知");

}

參考鏈接:

http://www.cocoachina.com/ios/20150112/10901.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS中通知機(jī)制又叫消息機(jī)制,其包括兩類:一類是本地通知;另一類是推送通知,也叫遠(yuǎn)程通知。兩種通知在iOS中...
    七里汀閱讀 2,171評(píng)論 3 2
  • 一、簡(jiǎn)介 目前說(shuō)的是基于ios10之前,所以有些類是過(guò)期的. 推送分為本地推送和遠(yuǎn)程推送2種。可以在應(yīng)用沒(méi)有打開(kāi)甚...
    JingYa_Lu閱讀 969評(píng)論 0 0
  • - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn ...
    nothing_c閱讀 206評(píng)論 0 0
  • iOS本地推送 第一步:創(chuàng)建本地推送 // 創(chuàng)建一個(gè)本地推送 UILocalNotifica...
    學(xué)無(wú)止境666閱讀 1,218評(píng)論 2 5
  • Test Trettff
    pku76閱讀 164評(píng)論 0 0

友情鏈接更多精彩內(nèi)容