计划应用程序本地通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计划应用程序本地通知相关的知识,希望对你有一定的参考价值。

Show a notification immediately after application enter in background (but you can schedule it too)
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. NSLog(@"application: didFinishLaunchingWithOptions:");
  3. // Override point for customization after application launch
  4.  
  5. UILocalNotification *localNotif = [launchOptions
  6. objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  7.  
  8. if (localNotif) {
  9. // has notifications
  10. }
  11. else {
  12. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  13. }
  14. [window makeKeyAndVisible];
  15. return YES;
  16. }
  17.  
  18. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif {
  19. NSLog(@"application: didReceiveLocalNotification:");
  20.  
  21. }
  22.  
  23. - (void)applicationDidEnterBackground:(UIApplication *)application {
  24.  
  25. UILocalNotification *localNotif = [[UILocalNotification alloc] init];
  26. localNotif.fireDate = [NSDate date]; // show now, but you can set other date to schedule
  27.  
  28. localNotif.alertBody = @"this is a notification!";
  29. localNotif.alertAction = @"notification"; // action button title
  30.  
  31. localNotif.soundName = UILocalNotificationDefaultSoundName;
  32.  
  33. // keep some info for later use
  34. NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"item-one",@"item", nil];
  35. localNotif.userInfo = infoDict;
  36.  
  37. [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
  38. [localNotif release];
  39. }

以上是关于计划应用程序本地通知的主要内容,如果未能解决你的问题,请参考以下文章

iOS-Unity 中的计划本地通知

如何阻止计划的本地通知在特定日期后触发?

本地通知在计划外的时间多次触发

切换启用/禁用本地通知(swift 4)

React Native 中的预定本地通知

iOS如何在本地通知上启动后台任务