iOS開發——本地通知

來源:互聯網
上載者:User

標籤:

本地通知是由本地應用觸發的,它是基於時間行為的一種通知形式,例如鬧鐘定時、待辦事項提醒,又或者一個應用在一段時候後不使用通常會提示使用者使用此應用等都是本地通知。建立一個本地通知通常分為以下幾個步驟:

  1. 建立UILocalNotification。 
  2. 設定處理通知的時間fireDate。 
  3. 配置通知的內容:通知主體、通知聲音、表徵圖數字等。 
  4. 配置通知傳遞的自訂資料參數userInfo(這一步可選)。 
  5. 調用通知,可以使用scheduleLocalNotification:按計劃調度一個通知,也可以使用presentLocalNotificationNow立即調用通知。

下面就以一個程式更新後使用者長期沒有使用的提醒為例對本地通知做一個簡單的瞭解。在這個過程中並沒有牽扯太多的介面操作,所有的邏輯都在AppDelegate中:進入應用後如果沒有註冊通知,需要首先註冊通知請求使用者允許通知;一旦調用完註冊方法,無論使用者是否選擇允許通知此刻都會調用應用程式的- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings代理方法,在這個方法中根據使用者的選擇:如果是允許通知則會按照前面的步驟建立通知並在一定時間後執行。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];        _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];        //設定全域導航條風格和顏色    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];       ViewController *mainController=[[ViewController alloc]init];    _window.rootViewController=mainController;        [_window makeKeyAndVisible];    if ([[UIApplication  sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {        [self addLocation];            }else{        [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings  settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];    }    return YES;}-(void)addLocation{    //定義本地通知對象    UILocalNotification *notification=[[UILocalNotification alloc]init];    //設定調用時間    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知觸發的時間,10s以後    notification.repeatInterval=4;//通知重複次數    //notification.repeatCalendar=[NSCalendar currentCalendar];//當前日曆,使用前最好設定時區等資訊以便能夠自動同步時間        //設定通知屬性    [email protected]"最近添加了諸多有趣的特性,是否立即體驗?"; //通知主體    notification.applicationIconBadgeNumber=1;//應用程式圖示右上方顯示的訊息數    [email protected]"開啟應用"; //待機介面的滑動動作提示    [email protected]"Default";//通過點擊通知開啟應用時的啟動圖片,這裡使用程式啟動圖片    //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知時播放的聲音,預設訊息聲音    [email protected]"msg.caf";//通知聲音(需要真機才能聽到聲音)        //設定使用者資訊    [email protected]{@"id":@1,@"user":@"Kenshin Cui"};//綁定到通知上的其他附加資訊        //調用通知    [[UIApplication sharedApplication] scheduleLocalNotification:notification];        }-(void)removeNotification{    [[UIApplication sharedApplication] cancelAllLocalNotifications];}-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{    if (notificationSettings.types!= UIUserNotificationTypeNone) {        [self  addLocation];    }}

 

iOS開發——本地通知

聯繫我們

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