IOS 本地推送(UILocalNotification)


推送通知

注意:這里說的推送通知NSNotification有所區別
NSNotification是抽象的,不可見的
推送通知是可見的(能用肉眼看到)

iOS中提供了2種推送通知

  • ●  本地推送通知(Local Notification)

  • ●  遠程推送通知(Remote Notification)

 

推送通知的作用

 推送通知的作用

可以讓不在前台運行的app,告知用戶app內部發生了什么事情

 

推送通知的呈現效果總結
總結一下,推送通知有5種不同的呈現效果
在屏幕頂部顯示一塊橫幅(顯示具體內容)
在屏幕中間彈出一個UIAlertView(顯示具體內容)
在鎖屏界面顯示一塊橫幅(鎖屏狀態下,顯示具體內容)
更新app圖標的數字(說明新內容的數量)
播放音效(提醒作用)

 

 

推送通知的使用細節

 發出推送通知時,如果程序正運行在前台,那么推送通知就不會被呈現出來

點擊推送通知后,默認會自動打開發出推送通知的app
不管app打開還是關閉,推送通知都能如期發出

 

本地推送通知
什么是本地推送通知
顧名思義,就是不需要聯網就能發出的推送通知(不需要服務器的支持)

 

本地推送通知的使用場景
常用來定時提醒用戶完成一些任務,比如
清理垃圾、記賬、買衣服、看電影、玩游戲

 

 

如何發出本地推送通知

創建本地推送通知對象
UILocalNotification *ln = [[UILocalNotification alloc] init];

設置本地推送通知屬性
推送通知的觸發時間(何時發出推送通知)

@property(nonatomic,copy) NSDate *fireDate;
推送通知的具體內容
@property(nonatomic,copy) NSString *alertBody;
鎖屏界面顯示的小標題(完整小標題:滑動來+ alertAction)

@property(nonatomic,copy) NSString *alertAction;
音效文件名
@property(nonatomic,copy) NSString *soundName;
app圖標數字
@property(nonatomic) NSInteger applicationIconBadgeNumber;

 

調度本地推送通知(調度完畢后,推送通知會在特地時間fireDate發出)
[[UIApplication sharedApplication] scheduleLocalNotification:ln];

 

得被調度的所有本地推送通知(等待發出的通知)

@property(nonatomic,copy) NSArray *scheduledLocalNotifications;

(已經發出且過期的推送通知就算調度結束,會自動從這個數組中移除)

取消調度本地推送通知
- (void)cancelLocalNotification:(UILocalNotification *)notification;

- (void)cancelAllLocalNotifications;

立即發出本地推送通知(使用價值:app在后台運行的時候)

- (void)presentLocalNotificationNow:(UILocalNotification *)notification;

本地推送通知的其他屬性

每隔多久重復發一次推送通知
@property(nonatomic) NSCalendarUnit repeatInterval;

點擊推送通知打開app時顯示的啟動圖片

@property(nonatomic,copy) NSString *alertLaunchImage;

附加的額外信息
@property(nonatomic,copy) NSDictionary *userInfo;

時區
@property(nonatomic,copy) NSTimeZone *timeZone;

(一般設置為[NSTimeZone defaultTimeZone] ,跟隨手機的時區)

 

 

點擊本地推送通知

當用戶點擊本地推送通知,會自動打開app,這里有2種情況

app並沒有關閉,一直隱藏在后台

app進入前台,並會調用AppDelegate的下面方法(並非重新啟動app)

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

app已經被關閉(進程已死)
啟動app,啟動完畢會調用AppDelegate的下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; 

launchOptions參數通過UIApplicationLaunchOptionsLocalNotificationKey 取出本地推送通知對象

 

 實例:

#import "HMAppDelegate.h"
#import "HMViewController.h"

@interface HMAppDelegate()
@property (nonatomic, weak) UILabel *label;
@end

@implementation HMAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
//    NSLog(@"------didFinishLaunchingWithOptions---%@");
    
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor redColor];
    label.frame = CGRectMake(0, 100, 200, 100);
    label.font = [UIFont systemFontOfSize:11];
    label.numberOfLines = 0;
    [[[self.window.rootViewController.childViewControllers firstObject] view] addSubview:label];
    
    UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if (note) {
        label.text = @"點擊本地通知啟動的程序";
        
        HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
        [homeVc performSegueWithIdentifier:@"home2detail" sender:note];
    } else {
        label.text = @"直接點擊app圖標啟動的程序";
    }
    self.label = label;
    return YES;
}

/**
 *  當用戶點擊本地通知進入app的時候調用 、通知發出的時候(app當時並沒有被關閉)
 */
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//    self.label.text = [NSString stringWithFormat:@"點擊通知再次回到前台---%d", application.applicationState];
    // 程序正處在前台運行,直接返回
    if (application.applicationState == UIApplicationStateActive) return;
    
    HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
    [homeVc performSegueWithIdentifier:@"home2detail" sender:notification];
}

@end
View Code
#import "HMViewController.h"
#import "HMDetailViewController.h"

@interface HMViewController ()
- (IBAction)schedule;
- (IBAction)cancel;
@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)schedule2 {
    // 1.創建本地推送通知對象
    UILocalNotification *ln = [[UILocalNotification alloc] init];
    
    // 2.設置通知屬性
    
    // 音效文件名
    ln.soundName = @"buyao.wav";
    
    // 通知的具體內容
    ln.alertBody = @"重大新聞:xxxx xxxx被調查了....";
    
    // 鎖屏界面顯示的小標題("滑動來" + alertAction)
    ln.alertAction = @"查看新聞吧";
    
    // 設置app圖標數字
    ln.applicationIconBadgeNumber = 10;
    
    [[UIApplication sharedApplication] presentLocalNotificationNow:ln];
}

- (IBAction)schedule {
    // 1.創建本地推送通知對象
    UILocalNotification *ln = [[UILocalNotification alloc] init];
    
    // 2.設置通知屬性
    // 音效文件名
    ln.soundName = @"buyao.wav";
    
    // 通知的具體內容
    ln.alertBody = @"重大新聞:xxxx xxxx被調查了....";
    
    // 鎖屏界面顯示的小標題("滑動來" + alertAction)
    ln.alertAction = @"查看新聞吧";
    
    // 通知第一次發出的時間(5秒后發出)
    ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 設置時區(跟隨手機的時區)
    ln.timeZone = [NSTimeZone defaultTimeZone];
    
    // 設置app圖標數字
    ln.applicationIconBadgeNumber = 5;
    
    // 設置通知的額外信息
    ln.userInfo = @{
                    @"icon" : @"test.png",
                    @"title" : @"重大新聞",
                    @"time" : @"2014-08-14 11:19",
                    @"body" : @"重大新聞:答復后即可更換就肯定會盡快趕快回家的瘋狂估計很快將發的"
                    };
    
    // 設置啟動圖片
    ln.alertLaunchImage = @"Default";
    
    // 設置重復發出通知的時間間隔
//    ln.repeatInterval = NSCalendarUnitMinute;
    
    // 3.調度通知(啟動任務)
    [[UIApplication sharedApplication] scheduleLocalNotification:ln];
}

- (IBAction)cancel {
    NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;
    NSLog(@"%@", notes);
//    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note
{
    HMDetailViewController *detailVc = segue.destinationViewController;
    detailVc.userInfo = note.userInfo;
}
@end
View Code

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM