本地推送(通知)

//
//  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

时间: 2024-10-16 01:11:03

本地推送(通知)的相关文章

本地推送通知UILocalNotification

1 - (IBAction)schedule { 2 // 1.创建本地推送通知对象 3 UILocalNotification *ln = [[UILocalNotification alloc] init]; 4 5 // 2.设置通知属性 6 // 音效文件名 7 ln.soundName = @"buyao.wav"; 8 9 // 通知的具体内容 10 ln.alertBody = @"重大新闻:xxxx xxxx被调查了...."; 11 12 // 锁

本地推送通知在iOS8上的不同

iOS8的不同点 你如果把上面的程序运行在iOS8上,会爆出如下错误 预习01-本地推送通知[掌握][615:7847] Attempting to schedule a local notification {fire date = Monday, July 13, 2015 at 9:02:25 AM China Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotification

本地推送通知小demo

本地推送通知: #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 在iOS8之后,Apple对用户隐私要求更加严格,所

本地推送通知界面跳转demo

本地推送通知界面跳转demo: /* 1.在发送本地通知的时候,通过userInfo属性来指示跳转到那个界面 2.监听本地通知的接收 1.当收到本地通知就会调用该代理方法 调用场景 1.如果应用程序在后台,当点击通知的时候 2.如果应用程序在前台,一旦收到本地通知,就会调用该方法 所以:判断如果应用程序在前台时候,不要执行界面跳转,来提高用户体验 3.如果应用程序被杀死了,那么这个方法就不再执行了 //- (void)application:(UIApplication *)applicatio

如何发送本地推送通知

如何发送本地推送通知 推送通知也属于UI的一部分,所以推送通知对象是以UI开头 方法送通知的代码方法控制器的-touchesBegan: withEvent: 中测试,比较合适,放到viewDidLoad方法,用户的注册请求还没有完成方法就调用了 创建本地通知 // 创建本地通知对象 UILocalNotification *ln = [[UILocalNotification alloc] init]; 设置本地通知属性(推荐一个一个属性测试运行) // 1.设置通知的内容(如果此属性不设置是

Swift 本地推送通知UILocalNotification

Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotification由全局的NotificationManager统一管理,我们只需要将本地通知对象添加到系统的Notification队列中就可以了,系统会在指定的时间激发本地通知. 本地推送通知:UILocalNotification 如果要使用推送通知,必须先在苹果的推送通知服务里注册你要使用哪几种类型的

(七十三)iOS本地推送通知的实现

iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被杀掉后是无法执行的,这时候就要借助远程通知,通过苹果的服务器转发通知到手机,本文只介绍本地通知的用法. ①对于iOS8及以上的版本,需要注册本地通知才能使用,一般在AppDelegate中注册: if ([[UIDevice currentDevice].systemVersion doubleVa

ios开发之-本地推送通知

不多说直接上代码 #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after

cocos2dx 加本地推送通知

SystemHelper::createLocalNotification(someTask.seconds, SystemHelper::getLocalizedText("Task_Finish")); void SystemHelper::createLocalNotification(unsigned long time,const char *msg) { UILocalNotification* notif = [[UILocalNotification alloc] in