ios--本地通知

來源:互聯網
上載者:User

標籤:

  1. ios8發送本地通知,需要先註冊本地通知。
  2. if ([[UIApplication sharedApplication]currentUserNotificationSettings].types==UIUserNotificationTypeNone) 當前應用的本地通知類型,UIUserNotificationTypeNone沒有註冊本地通知     註冊是系統級的,跟程式沒有關係,當程式刪除之後重新安裝依然是授權過了,綁定了id。

    //每次執行授權操作的時候,都會走這個代理- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings; 
  1.     UIUserNotificationTypeNone    = 0,      // 申請不得在收到通知時提出任何使用者介面
  2.     UIUserNotificationTypeBadge   = 1 << 0, // 該應用程式可在收到通知時將其表徵圖標記為
  3.     UIUserNotificationTypeSound   = 1 << 1, // 該應用程式可在收到通知後發出聲音
  4.     UIUserNotificationTypeAlert   = 1 << 2, // 該應用程式可在收到通知後顯示警報
  5. (對應標記,聲音,橫幅)

 

 

實際的開發分為兩種情況1.當應用程式退出到後台,通過點擊推送進入程式。這個時候可以通過使用-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;根據這個方法擷取推送的資訊。2.當應用程式被殺死的時候,點擊推送進來的時候會重新載入介面。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;可以根據launchOptions擷取推送內容。//接收通知參數UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];

  

-(void)addNotification{    UILocalNotification *notification=[[UILocalNotification alloc]init];    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];    [email protected]"通知來咯";    [[UIApplication sharedApplication]scheduleLocalNotification:notification];}

  

實現一個小功能:當使用者連續1小時不操作會通過本地推送提示使用者操作,每隔1小時提示一次。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//通知授權    if ([[UIApplication sharedApplication]currentUserNotificationSettings].types==UIUserNotificationTypeNone) {        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];    }}//失去焦點-(void)applicationWillResignActive:(UIApplication *)application{    [self addNotification];}-(void)addNotification{    UILocalNotification *notification=[[UILocalNotification alloc]init];    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];    [email protected]"一分鐘沒有用了";    notification.repeatInterval=NSCalendarUnitMinute;//設定重複的頻率        [[UIApplication sharedApplication]scheduleLocalNotification:notification];}//清空所有的本地通知 [[UIApplication sharedApplication]cancelAllLocalNotifications];

  

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.