iOS中 本地通知/本地通知詳解 韓俊強的部落格

來源:互聯網
上載者:User

iOS中 本地通知/本地通知詳解 韓俊強的部落格

Notification是智能手機應用編程中非常常用的一種傳遞資訊的機制,而且可以非常好的節省資源,不用消耗資源來不停地檢查資訊狀態(Pooling),在iOS下應用分為兩種不同的Notification種類,本地和遠程。本地的Notification由iOS下NotificationManager統一管理,只需要將封裝好的本地Notification對象加入到系統Notification管理機制隊列中,系統會在指定的時間激發將本地Notification,應用只需設計好處理Notification的方法就完成了整個Notification流程了。

本地Notification所使用的對象是UILocalNotification,UILocalNotification的屬性涵蓋了所有處理Notification需要的內容。UILocalNotification的屬性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。

1.首先要明白模擬器和真機的區別:模擬器不會有音頻提示,另外就是沒有檢測允許接受通知,所以我補充一下幾點:

1.添加監測通知:

 

 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){                [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];    }

上代碼:

 

 

#import "ViewController.h"#import "DetailViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIButton *schedule;@property (weak, nonatomic) IBOutlet UIButton *unSchedule;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}// 調度通知- (IBAction)schedule:(UIButton *)sender {       // 1.建立通知    UILocalNotification *ln = [[UILocalNotification alloc]init];        if (ln) {        // 設定時區        ln.timeZone = [NSTimeZone defaultTimeZone];        // 通知第一次發出的時間        ln.fireDate = [[NSDate date]dateByAddingTimeInterval:5];                // 2.設定通知屬性        ln.soundName = @"click.wav"; // 音效檔案名稱        // 通知的具體內容        ln.alertBody = @"重大新聞:小韓哥的部落格又更新了,趕快進來看看吧!....";                // 鎖定畫面顯示的小標題,完整標題:(“滑動來”+小標題)        ln.alertAction = @"查看新聞吧";                // 設定app表徵圖數字        ln.applicationIconBadgeNumber = 10;                // 設定app的額外資訊        ln.userInfo = @{                        @"icon":@"text.png",                        @"title":@"重大新聞",                        @"time":@"2016-02-28",                        @"body":@"重大新聞:小韓哥的部落格又更新了,趕快進來看看吧!"                        };        // 設定重啟圖片        ln.alertLaunchImage = @"101339g76j7j9t2zgzdvkj.jpg";               // 設定重複發出通知的時間間隔//        ln.repeatInterval = NSCalendarUnitMinute;                // 3.調度通知(啟動任務,在規定的時間發出通知)        [[UIApplication sharedApplication]scheduleLocalNotification:ln];        // 直接發出通知沒意義//        [[UIApplication sharedApplication]presentLocalNotificationNow:ln];    }    }- (IBAction)noSchedule:(UIButton *)sender{//    [[UIApplication sharedApplication]cancelAllLocalNotifications];    // 已經發出且到期的通知會從數組裡自動移除    NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;    NSLog(@"%@",notes);}- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note{    DetailViewController *detailVC = segue.destinationViewController;    detailVC.userInfo = note.userInfo;}@end

2.通知詳情版面設定基本屬性:

.h#import @interface DetailViewController : UIViewController@property (nonatomic, strong) NSDictionary *userInfo;@end.m#import "DetailViewController.h"@interface DetailViewController ()@property (weak, nonatomic) IBOutlet UILabel *userInfoContent;@end@implementation DetailViewController- (void)viewDidLoad {    [super viewDidLoad];        self.userInfoContent.text = self.userInfo[@"body"];}- (void)setUserInfo:(NSDictionary *)userInfo{    _userInfo = userInfo;}@end

3.didFinishLaunchingWithOptions 即時監測:

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //註冊本地通知        if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){                [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];    }//    NSLog(@"-----didFinishLaunchingWithOptions---");    UILabel *label = [[UILabel alloc]init];    label.frame = CGRectMake(0, 64, 320, 100);    label.backgroundColor = [UIColor redColor];    label.font = [UIFont systemFontOfSize:11];    label.numberOfLines = 0;    label.textColor = [UIColor whiteColor];    label.text = [launchOptions description];    [[[self.window.rootViewController.childViewControllers firstObject] view]addSubview:label];        UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsURLKey];    if (note) {        label.text = @"點擊本地通知啟動的程式";    }else{        label.text = @"直接點擊app表徵圖啟動的程式";    }    self.label = label;    return YES;}/** * 當使用者點擊本地通知進入app的時候調用(app當時並沒有被關閉) * 若app已關閉不會被調用此方法 */- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    self.label.text = @"點擊通知再次回到前台";    ViewController *homeVC = [self.window.rootViewController.childViewControllers firstObject];//    [homeVC performSegueWithIdentifier:@"toHome" sender:notification];    [homeVC performSegueWithIdentifier:@"toHome" sender:notification];    }
三種情況展示:(重要)

1.程式運行在後台

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.