#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. LAMP架构搭建+Discuz论坛搭建【weber出品必属精品】

    一.     本机简介: 本机系统: CentOS-6.4-x86_64 主机名:oracle.ywb IP地址:192.168.146.129 二.     在Linux环境下安装Apache步骤 ...

  2. ios--图片处理(修改、保存)UIGraphicsBeginImageContext

    http://www.th7.cn/Program/IOS/201407/250904.shtml

  3. Visual C++基础知识(win32exe)

    1.Visual C++简称VC或者VC++ 是一个集成开发环境(编辑器+调试器+编译器) gcc---Linux和Unix的C++编译器 Vc----Windows的C++编译器 2.MFC(Mic ...

  4. Oracle11g R2学习系列 之一安装篇

    前言 最近想去前同事推荐的一个Oracle相关的岗位竞争一下,由于之前没有使用Oracle的相关经验,所以决定学习一下,就算最终岗位应聘不成,至少也有了一些Oracle经验了,说不定哪天就用上了.再说 ...

  5. Windows软件使用Q&A集锦【持续更新】

    以下不注明原创的均为转载,感谢原作者,希望大家电脑用的都舒心 Q: QQ电脑管家的默认程序的程序推荐如何关闭?我右键点击文件打开方式,选择默认程序的时候,qq管家总弹出来,还给我推荐程序.如何关闭? ...

  6. ExpandableListView 保证只展开一组

    expandableListView.setOnGroupExpandListener(new OnGroupExpandListener(){ @Override public void onGro ...

  7. MessagerService总结

    一.整体工程图 二.messenger_service_binding.xml <?xml version="1.0" encoding="utf-8"? ...

  8. 如何允许外网可以连接mysql数据库

    1.首先检查mysql所在服务器的防火墙,如果限制了外网对3306端口的连接,那么放开限制Linux服务器中执行 iptables -L   可以查看当前的防火墙规则iptables -F   可以清 ...

  9. linux mysql密码破解一张图解释

  10. mips平台使用jdbc操作sqlite的最终解决方案

    1.概述: 本项目需要在多平台上(mips必须支持)操作嵌入式数据库sqlite,而最新的sqlite-jdbc-3.15.1.jar的本地驱动只含有少量的平台,于是解决支持mips平台实在必行.方法 ...