zoukankan      html  css  js  c++  java
  • 本地推送(通知)

    //
    //  AppDelegate.m
    //  LocalNotification
    //
    //  Created by xiaoyao on 15/3/17.
    //  Copyright (c) 2015年 lijien. All rights reserved.
    //
    
    #import "AppDelegate.h"
    #import "LocalNotificationController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      // Override point for customization after application launch.
      self.window.backgroundColor = [UIColor whiteColor];
      
      [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
      [[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
      
      //
      if (launchOptions) {
        UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (localNotification) {
          NSString *msg = [localNotification.userInfo objectForKey:@"key"];
          UIAlertView *alert = [[UIAlertView  alloc] initWithTitle:@"本地通知(后台)"
                                                           message:msg delegate:nil
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
          [alert show];
        }
      }
      
      LocalNotificationController *loc = [[LocalNotificationController alloc] init];
      self.window.rootViewController = loc;
      
      [self.window makeKeyAndVisible];
      return YES;
    }
    
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
      if (notificationSettings.types != UIUserNotificationTypeNone) {
        
      }
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
      // 应用程序进入前台之后取消角标
      [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
      if (notification) {
        NSString *msg = [notification.userInfo objectForKey:@"key"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"
                                                            message:msg
                                                           delegate:nil
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确定", nil];
        [alertView show];
        
      }
    }
    @end
    
    //
    //  LocalNotificationController.m
    //  LocalNotification
    //
    //  Created by xiaoyao on 15/3/17.
    //  Copyright (c) 2015年 lijien. All rights reserved.
    //
    
    #import "LocalNotificationController.h"
    
    @interface LocalNotificationController ()
    
    @end
    
    @implementation LocalNotificationController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
      
      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      btn.frame = CGRectMake(100, 200, 100, 50);
      btn.backgroundColor = [UIColor grayColor];
      [btn setTitle:@"发送通知" forState:UIControlStateNormal];
      btn.titleLabel.textColor = [UIColor blueColor];
      [btn addTarget:self action:@selector(addLocalNotification) forControlEvents:UIControlEventTouchUpInside];
      [self.view addSubview:btn];
    }
    
    #pragma mark - 加入本地通知
    - (void)addLocalNotification {
      // 已经同意授权的本地通知则创建本地通知,否则先得到本地通知授权以便设置通知项
      if ([UIApplication sharedApplication].currentUserNotificationSettings.types == UIUserNotificationTypeNone) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:UIUserNotificationTypeAlert|
                                                                             UIUserNotificationTypeBadge|
                                                                             UIUserNotificationTypeSound
                                                                             categories:nil]];
      }
      // 先取消之前假设加入过得本地通知
      [[UIApplication sharedApplication] cancelAllLocalNotifications];
      
      UILocalNotification *notification = [[UILocalNotification alloc] init];
      notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
      // 设置反复次数
      notification.repeatInterval = 2;
      
      // 设置通知的属性
      notification.alertBody = @"近期加入了非常多有趣的应用,快点击看哦!";
      notification.alertAction = @"打开应用";
      notification.alertLaunchImage = @"Default";
      notification.applicationIconBadgeNumber = 1;
      notification.userInfo = @{@"key" : @"lije"};
      
      // 调用通知注冊
      [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    
    - (void)removeNotifitications {
      [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }
    
    #pragma mark - 移除通知
    - (void) dealloc {
      [self removeNotifitications];
    }
    @end
    

  • 相关阅读:
    CoreBluetooth
    IOS Monkey 测试
    Ruby+appium实现截图、滑屏、长按、日志输出到本地文件夹
    maven中GroupID 和ArtifactID怎么写
    MAC安装Eclipse及对其进入相关配置
    单元测试断言利器 AssertJ
    python+appium app自动化的方法实例运用
    美团接口自动化测试实践
    appium滑动操作总结
    Appium+python自动化-Appium Python API
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7154167.html
Copyright © 2011-2022 走看看