iOS的本地推送UILocalNotification的使用

來源:互聯網
上載者:User

標籤:

UILocalNotification

 

 

  1 第一步:接收本地推送  2   3 實現代理方法didReceiveLocalNotification  4   5     - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{  6   7 //在此時設定解析notification,並展示提示視圖  8   9 } 10  11 第二步:建立本地推送 12 - (void)createLocalNotification { 13  14 // 建立一個本地推送 15  16  17 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 18  19  20  21 //設定10秒之後 22  23  24  25 NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; 26  27 if (notification != nil) { 28  29 // 設定推送時間 30  31 notification.fireDate = pushDate; 32  33   // 設定時區 34  35 notification.timeZone = [NSTimeZone defaultTimeZone]; 36  37 // 設定重複間隔 38  39 notification.repeatInterval = kCFCalendarUnitDay; 40 // 推送聲音 41 notification.soundName = UILocalNotificationDefaultSoundName; 42 // 推送內容 43 notification.alertBody = @"推送內容"; 44 //顯示在icon上的紅色圈中的數子 45 notification.applicationIconBadgeNumber = 1; 46 //設定userinfo 方便在之後需要撤銷的時候使用 47  48 NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"]; 49  50 notification.userInfo = info; 51  52 //添加推送到UIApplication 53  54 UIApplication *app = [UIApplication sharedApplication]; 55  56 [app scheduleLocalNotification:notification]; 57  58 } 59  60 } 61  62 第三步:解除本地推送 63  64  - (void) removeLocalNotication { 65  66 // 獲得 UIApplication 67 UIApplication *app = [UIApplication sharedApplication]; 68  69 //擷取本地推送數組 70 NSArray *localArray = [app scheduledLocalNotifications]; 71  72  73  74 //聲明本地通知對象 75 UILocalNotification *localNotification; 76  77 if (localArray) { 78  79 for (UILocalNotification *noti in localArray) { 80  81 NSDictionary *dict = noti.userInfo; 82  83 if (dict) { 84  85 NSString *inKey = [dict objectForKey:@"key"]; 86  87 if ([inKey isEqualToString:@"對應的key值"]) { 88  89 if (localNotification){ 90  91 [localNotification release]; 92  93 localNotification = nil; 94 } 95  96 localNotification = [noti retain]; 97  98 break; 99 100 }101 102 }103 104 }105 106 107 //判斷是否找到已經存在的相同key的推送108 if (!localNotification) {109 110 //不存在初始化111 localNotification = [[UILocalNotification alloc] init];112 113 }114 if (localNotification) {115 //不推送 取消推送116 [app cancelLocalNotification:localNotification];117 [localNotification release];118 119 return;120 }121 122 }123 124 125 }

 

iOS的本地推送UILocalNotification的使用

聯繫我們

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