ios 本地推送 - 提醒功能

來源:互聯網
上載者:User

標籤:

當使用者離開應用程式後建立一個本地訊息。
如果使用者在使用應用程式時需要設定一些配置資訊,請謹慎使用類訊息。

假設一個應用程式在前台運行。當應用程式通過Home鍵切換到後台運行時,1秒鐘後,能見到一下視窗彈出資訊,(ios 5 和 ios 4裝置)。





如果使用者點擊這條資訊,則可以讓應用程式恢複到前台運行。

實現代碼非常簡單,在一開始建立一個NSDate 對象,記錄當前的日期和時間,添加1秒鐘在這個對象上,
設定本地訊息的觸發時間。設定本地的通知訊息和包裹,與作業系統調度通知。

在 delegate.m檔案中加入以下代碼:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
  UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];

  // Current date
  NSDate *date = [NSDate date]; 

  // Add one second to the current time
  NSDate *dateToFire = [date dateByAddingTimeInterval:1];

  // Set the fire date/time
  [localNotification setFireDate:dateToFire];
  [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];    

  // Setup alert notification
  [localNotification setAlertBody:@"Tap to return to TestApp" ];
  [localNotification setAlertAction:@"Open TestApp"];
  [localNotification setHasAction:YES];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}


當彈出的訊息框,使用者點擊後,以下的方法內可以在應用程式恢複到前台的時候執行一次
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  // Your code here...
}

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.