iOS8 本地推送 UILocalNotification

前言

最近忙中偷閑细疚,把有關(guān)本地推送的內(nèi)容給整理一下食听,本篇文章主要講述UILocalNotification的一些使用,下篇文章會在此基礎(chǔ)上介紹其替代品UNUserNotificationCenter羽嫡。

UILocalNotification的使用

在iOS8及其之后的推送都使用UILocalNotification來實現(xiàn)芦鳍,有關(guān)于UILocalNotification的介紹我們可以在其框架中可見。

// In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to schedule and present UILocalNotifications
NS_CLASS_DEPRECATED_IOS(4_0, 10_0, "Use UserNotifications Framework's UNNotificationRequest") __TVOS_PROHIBITED
@interface UILocalNotification : NSObject<NSCopying, NSCoding>

在官方的解釋中指出了UILocalNotification應(yīng)該如何使用:在iOS8.0及更高的版本中僻族,你的應(yīng)用程序如果想要顯示通知就必須使用-[UIApplication registerUserNotificationSettings:]注冊用戶通知

那么到此為止,我們關(guān)于本地推送的任務(wù)只需要兩步就可以搞定了:

  1. 使用-[UIApplication registerUserNotificationSettings:]注冊通知;
  2. 實現(xiàn)推送通知內(nèi)容

1. 使用-[UIApplication registerUserNotificationSettings:]注冊通知

注冊通知的位置可以隨意魂爪,只要步驟2之前就可以了。不過習(xí)慣性我們都會講注冊通知一類的放在程序啟動的時候康嘉,即放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函數(shù)中。

在上述的官方解釋中提到了注冊通知是要使用-[UIApplication registerUserNotificationSettings:]來實現(xiàn)的籽前,又因為其是個實例方法(-方法)亭珍,故應(yīng)使用UIApplication的實例對象調(diào)用該方法。

[[UIApplication sharedApplication] registerUserNotificationSettings:<#(nonnull UIUserNotificationSettings *)#>];

函數(shù)調(diào)用出來了枝哄,問題來了肄梨,這里調(diào)用的-registerUserNotificationSettings:這個函數(shù)還需要一個UIUserNotificationSettings類型的參數(shù),我們來查看一下這個類挠锥。

@interface UIUserNotificationSettings : NSObject

// categories may be nil or an empty set if custom user notification actions will not be used
+ (instancetype)settingsForTypes:(UIUserNotificationType)types
                      categories:(nullable NSSet<UIUserNotificationCategory *> *)categories; // instances of UIUserNotificationCategory

@property (nonatomic, readonly) UIUserNotificationType types;

// The set of UIUserNotificationCategory objects that describe the actions to show when a user notification is presented
@property (nullable, nonatomic, copy, readonly) NSSet<UIUserNotificationCategory *> *categories;

@end

UIUserNotificationSettings類繼承自NSObject众羡,有一個類方法(+方法)用以初始化,此方法中包含了兩個參數(shù)types和categories蓖租。

  1. types: 這個參數(shù)是一個UIUserNotificationType的枚舉,如下所示:
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
    UIUserNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    UIUserNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received
    UIUserNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received
    UIUserNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's UNAuthorizationOptions") __TVOS_PROHIBITED;

其具體意思就是說當(dāng)應(yīng)用程序收到通知的時候是否要更改顯示程序角標(biāo)粱侣、是否播放聲音、是否彈框提醒等蓖宦。

  1. categories: 這個參數(shù)在類方法的說明中就已經(jīng)說明了齐婴,如果不適用自定義用戶通知操作,可以設(shè)為nil稠茂。

到這里推送的注冊代碼就已經(jīng)可以寫出來了:

// 此種通知只適用于iOS8.0及更高版本柠偶,故加個判斷
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 設(shè)置通知類型
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
        // 授權(quán)通知
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    }

2. 實現(xiàn)推送通知內(nèi)容

要使用UILocalNotification實現(xiàn)推送功能,我們首先可以查看一下該類的組成:

@interface UILocalNotification : NSObject<NSCopying, NSCoding>

// 初始化方法
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// timer-based scheduling
// 推送發(fā)送時間
@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).
// 推送時間所屬時區(qū)的設(shè)置
@property(nullable, nonatomic,copy) NSTimeZone *timeZone;

// 重復(fù)時間
@property(nonatomic) NSCalendarUnit repeatInterval;      // 0 means don't repeat
@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.
@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.
@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
@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
@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
@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

該類中包含了兩個初始化方法- init- initWithCoder:以及很多的成員變量睬关,這些成員變量就是發(fā)送通知的一些設(shè)置诱担。關(guān)于部分成員變量的作用我直接在上面的代碼中標(biāo)注了(有些英文標(biāo)注很清楚,就此略過)电爹。下面就可以直接創(chuàng)建通知蔫仙。

            // 創(chuàng)建通知
            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            // 設(shè)置通知的必選參數(shù)
            // 設(shè)置通知顯示的內(nèi)容
            localNotification.alertBody = @"我來找你咯";
            // 設(shè)置通知發(fā)送的時間(這里設(shè)置的是當(dāng)前時間延遲10s發(fā)送,創(chuàng)建通知我是放在按鈕點擊事件中了丐箩,點擊之后摇邦,按home鍵等待10s即可收到推送)
            localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
            // 解鎖滑動時的事件
            localNotification.alertAction = @"磨蹭啥呢";
            // 收到推送通知時APP icon角標(biāo),這里如果設(shè)置了雏蛮,那么在應(yīng)用程序即將啟動的時候要記得把角標(biāo)清空
            localNotification.applicationIconBadgeNumber = 1;
            // 推送是帶有聲音提醒的涎嚼,設(shè)置默認(rèn)的字段為 UILocalNotificationDefaultSoundName
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            // 發(fā)送通知
            // 方式一:根據(jù)通知的發(fā)送時間(fireDate)發(fā)送通知
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            // 方式二:立即發(fā)送通知
//            [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

推送創(chuàng)建中還有很多參數(shù),比如推送消息中包含圖片等等挑秉,可以沒事自己嘗試一下,我這里只是搞了個最簡單的苔货。

總結(jié)

關(guān)于UILocalNotification實現(xiàn)本地推送的介紹就完了犀概。我介紹的時候并不是直接告訴你應(yīng)該怎么使用立哑,而是告訴你應(yīng)該怎么樣才能更好的去學(xué)習(xí)一個新的類(東西),并不是看看別人怎么用的姻灶,然后自己拿過來直接使用就OK了铛绰,那樣是不行的。

授人以魚不如授人以漁产喉。在你碰到一個新東西的時候捂掰,并不是要著急著去網(wǎng)上找關(guān)于這個東西的使用方法及其介紹,相反的我們可以自己通過查看其英文注釋(我英語很差曾沈,但是需要耐著性子慢慢看这嚣,遇到不認(rèn)識的單詞谷歌一下,慢慢的你就能看懂咯)或者了解類的構(gòu)造等各種方法來深入剖析其應(yīng)該怎么使用以及其功能等等塞俱。

我認(rèn)為我就是在一本正經(jīng)的胡說八道 ??????

友情鏈接:iOS10 本地推送 UNUserNotificationCenter

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末姐帚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子障涯,更是在濱河造成了極大的恐慌罐旗,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唯蝶,死亡現(xiàn)場離奇詭異九秀,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)粘我,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門颤霎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人涂滴,你說我怎么就攤上這事友酱。” “怎么了柔纵?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵缔杉,是天一觀的道長。 經(jīng)常有香客問我搁料,道長或详,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任郭计,我火速辦了婚禮霸琴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘昭伸。我一直安慰自己梧乘,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著选调,像睡著了一般夹供。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上仁堪,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天哮洽,我揣著相機(jī)與錄音,去河邊找鬼弦聂。 笑死鸟辅,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的莺葫。 我是一名探鬼主播匪凉,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼徙融!你這毒婦竟也來了洒缀?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤欺冀,失蹤者是張志新(化名)和其女友劉穎树绩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體隐轩,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡饺饭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了职车。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瘫俊。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖悴灵,靈堂內(nèi)的尸體忽然破棺而出扛芽,到底是詐尸還是另有隱情,我是刑警寧澤积瞒,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布川尖,位于F島的核電站,受9級特大地震影響茫孔,放射性物質(zhì)發(fā)生泄漏叮喳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一缰贝、第九天 我趴在偏房一處隱蔽的房頂上張望馍悟。 院中可真熱鬧,春花似錦剩晴、人聲如沸锣咒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宠哄。三九已至壹将,卻和暖如春嗤攻,著一層夾襖步出監(jiān)牢的瞬間毛嫉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工妇菱, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留承粤,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓闯团,卻偏偏與公主長得像辛臊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子房交,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 極光推送: 1.JPush當(dāng)前版本是1.8.2彻舰,其SDK的開發(fā)除了正常的功能完善和擴(kuò)展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,724評論 10 16
  • 推送通知 注意:這里說的推送通知跟NSNotification有所區(qū)別 NSNotification是抽象的候味,不可...
    iOS開發(fā)攻城獅閱讀 4,237評論 1 13
  • 概述 在多數(shù)移動應(yīng)用中任何時候都只能有一個應(yīng)用程序處于活躍狀態(tài)刃唤,如果其他應(yīng)用此刻發(fā)生了一些用戶感興趣的那么通過通知...
    莫離_焱閱讀 6,519評論 1 8
  • 推送通知注意:這里說的推送通知跟NSNotification有所區(qū)別NSNotification是抽象的,不可見的...
    醉葉惜秋閱讀 1,522評論 0 3
  • 許多集成的步驟個推官網(wǎng)都有了白群,這里只寫關(guān)于推送的遠(yuǎn)程推送和本地通知的步驟和代碼尚胞。APP在后臺時:走蘋果的APNS通...
    AllureJM閱讀 2,727評論 1 9