iOS本地通知(NSLocalNotification)的使用

來源:互聯網
上載者:User

效果圖:




圖一:


核心代碼:

////  JLViewController.m//  本地通知////  Created by XinYou on 15-5-15.//  Copyright (c) 2015年 vxinyou. All rights reserved.//#import "JLViewController.h"@interface JLViewController ()- (IBAction)addLocalNotification;@end@implementation JLViewController- (void)viewDidLoad{    [super viewDidLoad];    }/* 三種情況: 1.應用沒有被關閉,而是在後台運行著。    此時點擊通知進入應用,會調用JLAppDelegate的didReceiveLocalNotification方法,    而且如果有設定notification的alertLaunchImage屬性,會先顯示該屬性對應的圖片,然後進入應用。  2.應用處於關閉狀態,沒有在後台運行。    點擊應用表徵圖啟動應用,會調用JLAppDelegate的didFinishLaunchingWithOptions方法  3.應用處於關閉狀態,沒有在後台運行。    點擊通知啟動應用,也會調用JLAppDelegate的didFinishLaunchingWithOptions方法  那麼,該如何區分是通過 點擊應用表徵圖 還是通過 點擊通知 進入應用的呢。 具體參見JLAppDelegate的didFinishLaunchingWithOptions方法  *//** *  添加本地通知 */- (IBAction)addLocalNotification {    // 1.建立通知    UILocalNotification *notification = [[UILocalNotification alloc] init];        // 2.設定屬性        // 下面這句代碼的作用是:鎖屏的時候會顯示“滑動來開始玩遊戲”,如圖一所示。    notification.alertAction = @"開始玩遊戲";    // 通知的內容    notification.alertBody = @"屠龍寶刀,點擊就送!!!";        // 通知對應的提醒數字。當通知來臨,會在應用表徵圖的右上方有一個提醒數字    notification.applicationIconBadgeNumber = 1;        // 通知的重複時間間隔(枚舉類型)。這裡設定為每一個小時通知一次//    notification.repeatInterval = NSCalendarUnitHour;        // 一般設定為程式的啟動圖片(LaunchImage)。意思是通過點擊通知來重新進入應用時,需要顯示的圖片。//    notification.alertLaunchImage = @"Default";        // 通知時間。設定為4秒之後顯示通知    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:4.0];        // 這裡設定的userinfo,可以在JLAppDelegate.m檔案中的didFinishLaunchingWithOptions方法中獲得。    notification.userInfo = @{@"username" : @"zhangsan"};        // 3.註冊通知        UIApplication *application = [UIApplication sharedApplication];        // 先取消通知,避免重複添加    [application cancelLocalNotification:notification];        // 取消所有通知//    [application cancelAllLocalNotifications];        // 註冊(添加)通知    [application scheduleLocalNotification:notification];}@end////  JLAppDelegate.m//  本地通知////  Created by XinYou on 15-5-15.//  Copyright (c) 2015年 vxinyou. All rights reserved.//#import "JLAppDelegate.h"@implementation JLAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 如果launchOptions為空白,表示是通過點擊應用表徵圖的方式啟動程式    if (launchOptions == nil) {                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"正常啟動應用" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];                [alertView show];            }else{// 如果launchOptions不為空白,表示通過點擊通知的方式啟動程式            UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];                NSDictionary *userinfo = notification.userInfo;                NSString *username = userinfo[@"username"];                NSString *msg = [NSString stringWithFormat:@"點擊通知啟動應用!username:%@", username];                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];                [alertView show];    }        return YES;}/** *  使用者點擊通知進入本應用(應用並沒有被關閉,而是在後台運行著) */- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"點擊通知進入應用" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];        [alertView show];}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

源碼下載地址:https://github.com/liujie537192/LocalNotification

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.