博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
本地推送(通知)
阅读量:5148 次
发布时间:2019-06-13

本文共 5870 字,大约阅读时间需要 19 分钟。

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

转载于:https://www.cnblogs.com/yangykaifa/p/7154167.html

你可能感兴趣的文章
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
一种简单的数据库性能测试方法
查看>>
如何给JavaScript文件传递参数
查看>>
Hadoop HBase概念学习系列之物理视图(又名为物理模型)(九)
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
ExtJS 4.2 业务开发(一)主页搭建
查看>>
webpack Import 动态文件
查看>>
电脑没有安装iis,但是安装了.NET环境,如何调试网站发布的程序
查看>>
【Mac + GitHub】之在另一台Mac电脑上下载GitHub的SSH链接报错
查看>>
Day03:Selenium,BeautifulSoup4
查看>>
Java NIO系列教程(九) ServerSocketChannel
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
postgis几何操作函数集
查看>>
ACM题目————还是畅通工程
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
35. Search Insert Position(C++)
查看>>
ubuntu 卡在登陆界面无法进入桌面,但是可以进入命令行界面
查看>>