#import "AppDelegate.h"

#import "GlobalDefine.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

// 判断是否是8.0以上

if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {

UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];

category.identifier = NotificationIdentifier;

UIMutableUserNotificationAction *foregroundAction = [[UIMutableUserNotificationAction alloc] init];

foregroundAction.identifier = foregroundActionidentifier;

foregroundAction.title = @"前台模式";

foregroundAction.activationMode = UIUserNotificationActivationModeForeground;

UIMutableUserNotificationAction *backgroundAction = [[UIMutableUserNotificationAction alloc] init];

backgroundAction.identifier = backgroundActionidentifier;

backgroundAction.title = @"后台模式";

backgroundAction.activationMode = UIUserNotificationActivationModeBackground;

[category setActions:@[foregroundAction, backgroundAction] forContext:UIUserNotificationActionContextDefault];

NSSet *set = [NSSet setWithObjects:category, nil];

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories: set];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}

return YES;

}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler {

if ([identifier isEqualToString:foregroundActionidentifier]) {

NSLog(@"ios9点击了前台模式");

}

completionHandler();

}

@end

______________________________________华丽的分割线_________________________________________________

#import "ViewController.h"

#import "GlobalDefine.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

UILocalNotification *notification = [[UILocalNotification alloc] init];

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 推送时间

notification.alertTitle = @"推送";  // 推送标题

notification.alertBody = @"推送内容"; // 推送的内容

notification.category = NotificationIdentifier;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

@end

______________________________________华丽的分割线_________________________________________________

#ifndef GlobalDefine_h

#define GlobalDefine_h

#define NotificationIdentifier @"NotificationIdentifier"

#define backgroundActionidentifier @"backgroundAction.identifier"

#define foregroundActionidentifier @"foregroundAction.identifier"

#endif /* GlobalDefine_h */

PS: 最下面的 #ifndef GlobalDefine_h 头文件

可以全部复制进去运行,

iOS之本地推送(前台模式与后台模式)的更多相关文章

  1. iOS开发本地推送(iOS10)UNUserNotificationCenter

    1.简介 iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了.简单介绍本地推送的使用UserNotifications官方文档说明! 2.简单使用UNUs ...

  2. iOS开发本地推送

    1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...

  3. iOS的本地推送删除不了解决方法

    最近在研究苹果推送,当测试本地推送的时候,发现一个问题,就是一旦你添加了一个本地推动的通知,当你修改代码,删除应用,当你再次运行app,它还是会在横幅上面弹出推送,尼玛怎么搞都删除不了,近乎崩溃了,开 ...

  4. iOS关于本地推送

      不多说 直接上代码 
 NSDate *now = [NSDate date]; UILocalNotification *reminderNotification = [[UILocalNoti ...

  5. IOS中程序如何进行推送消息(本地推送,远程推送)

    [1]-------------什么是推送消息? 我就以一张图解释------------ [2]-----------IOS程序中如何进行本地推送?----------- 2.1,先征求用户同意 1 ...

  6. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  7. 81、iOS本地推送与远程推送详解

    一.简介 分为本地推送和远程推送2种.可以在应用没打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户石否同意,如果同意则正常使用:如果用户不同意则下次打开程序 ...

  8. ios 开发之本地推送

    网络推送可能被人最为重视,但是本地推送有时候项目中也会运用到: 闲话少叙,代码如下: 1.添加根视图 self.window.rootViewController = [[UINavigationCo ...

  9. IOS 本地推送(UILocalNotification)

    推送通知 ● 注意:这里说的推送通知跟NSNotification有所区别 • NSNotification是抽象的,不可见的 • 推送通知是可见的(能用肉眼看到) ● iOS中提供了2种推送通知 ● ...

随机推荐

  1. arry()数组的理解及api的使用(一)

    我们想要了解数组,首先就要先要了解到什么是数据结构,所谓的数据结构就是把数据与数据见的关系按照特定的结构来保存.设计合理的数据结构是解决问题的前提.了解了数据结构后我们下面来数组的定义:数组(arra ...

  2. 获取本机CPU,硬盘等使用情况

    早上的时候接到主管的一个任务,要获取服务器上的cpu,硬盘, 数据库等 的使用情况,并以邮件的方式发给boss, = =没办法,公司的服务器真是不敢恭维,顺便吐槽一下公司的网速,卡的时候30k左右徘徊 ...

  3. ecshop安全方面的一些参考建议

    一,ecshop安装,其实很简单,只要一直下一步下一步点击即可,这样总是没有错的,因为官方不可能给我们一个有问题的程序,尽量从简即可. 请注意一下两点 A:在安装ecshop的时候,不要将所有文件都设 ...

  4. TypeScript环境搭建

    环境搭建 本篇将简单介绍一下TypeScript,并记录开发环境的搭建.使用Visual Studio Code进行一个简单的Demo开发过程. 第一部分.简介 TypeScript是一种由微软开发的 ...

  5. SVN取消记住密码

    百度上面找的,SVN取消记录密码的方法: 找到这个文件夹首先需要在文件夹选项里面,显示所有的文件和文件夹,不能隐藏. SVN取消记住用户名的方法 打开C:\Documents and Settings ...

  6. LeetCode_N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  7. GetSystemTime API可以得到毫秒级时间

    用Now返回的日期格式中年只有2位,即2000年显示为00, 这似乎不太令人满意. 此外Now和Time都只能获得精确到秒的时间,为了得到更精确的毫秒级时间,可以使用API函数GetSystemTim ...

  8. 使用OpenSSL生成证书

    使用OpenSSL生成证书 下载安装openssl,进入/bin/下面,执行命令(把ssl目录下的openssl.cnf 拷贝到bin目录下)1.首先要生成服务器端的私钥(key文件):openssl ...

  9. 《数据通信与网络》笔记--SSL/TLS

    上次简单地介绍了IP层的安全,今天来介绍下在传输层提供安全性最主要的2个协议:安全套接字层(SSL)协议和传输 层安全(TLS)协议.TLS实际上就是SSL的IETF版本. 1.SSL服务 设计安全套 ...

  10. js深入研究之类定义与使用

    js可以定义自己的类 很有意思 <script type="text/javascript"> var Anim = function() { alert('nihao ...