使用UILocalNotification給App添加本地訊息通知

來源:互聯網
上載者:User

標籤:

使用過的代碼,直接貼上

 1 UILocalNotification *notification = [[UILocalNotification alloc] init]; 2   if (notification!=nil) { 3     NSDate *now = [NSDate new]; 4     //從現在開始,10秒以後通知 5     notification.fireDate=[now addTimeInterval:10]; 6     //使用本地時區 7     notification.timeZone=[NSTimeZone defaultTimeZone]; 8     notification.alertBody=@"頂部提示內容,通知時間到啦"; 9     //通知提示音 使用預設的10     notification.soundName= UILocalNotificationDefaultSoundName;11     notification.alertAction=NSLocalizedString(@"你鎖屏啦,通知時間到啦", nil);12     //這個通知到時間時,你的應用程式右上方顯示的數字。13     notification.applicationIconBadgeNumber = 1;14     NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];15     notification.userInfo = dic;16     //啟動這個通知17     [[UIApplication sharedApplication]scheduleLocalNotification:notification];18 }

需要注意的是在iOS8之後需要註冊訊息推送服務才可以,具體實現就在AppDelegate的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法中直接調用下面方法即可

1 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {2       [application registerUserNotificationSettings:[UIUserNotificationSettings 3                                    settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound 4                                          categories:nil]];5 }

 

使用UILocalNotification給App添加本地訊息通知

聯繫我們

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