iOS 推送通知有两种:本地推送、远程推送.

本地推送 :  在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟。本地通送有局限性在于当APP处于后台或者退出时就无法发出通知。

远程推送:   APNs和第三方推送,第三方推送最终也需要APNs转发,

本地推送实现
  注册通知:

    

float sysVer = [[UIDevice currentDevice].systemVersion floatValue];

    if (sysVer < 10) {

        //设置通知类型 弹框、脚标、声音

        UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

    }else{

        UNUserNotificationCenter* center =[UNUserNotificationCenter currentNotificationCenter];

        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {

            if (granted) {

                NSLog(@"通知注册成功");

            }

        }];

    }

  

  发送通知:
    

    

float sysVer = [[UIDevice currentDevice].systemVersion floatValue];

    if(sysVer < 10){

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

        //给这些属性赋值才能让通知有特定的内容

        local.alertBody=@"航海王:One Piece";

        //特定的时间让显示出来()

        NSString* dateStr = @"2016-12-01 16:30:00";

        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];

        dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss";

        local.fireDate=[NSDate dateWithTimeIntervalSinceNow:60];

        //滑动解锁的文字(在推送通知信息的下面一小行字)

        local.alertAction =@" ";

        //有声音给声音,没声音用默认的

        local.soundName=@"UILocalNotificationDefaultSoundName";

        //设置图标右上角数字

        local.applicationIconBadgeNumber=1;

        //用户信息

        local.userInfo=@{@"name":@"航海王",@"content":@"One Piece",@"time":@"20161201"};

        //3:定制一个通知 

        [[UIApplication sharedApplication]scheduleLocalNotification:local];

    }else{

        UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];

        content.title = [NSString localizedUserNotificationStringForKey:@"Hello Title" arguments:nil];

        content.body = [NSString localizedUserNotificationStringForKey:@"Hello Body" arguments:nil];

        content.sound = [UNNotificationSound defaultSound];

        //设定通知时间

        UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

        UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"notificationIdentifier" content:content trigger:trigger];

        UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

        }];

    }

远程推送实现

  服务器向客户端发送推送的过程:

                  

   每个请求中都有设备令牌,APNs通过设备令牌去识别设备和APP,每次运行APP,都会向服务器发送设备令牌,服务器再使用设备令牌发送推送,只有当推送设置改变时设备令牌才会被改变 ,只有APNs 可以解码设备令牌.

    

  服务器与APNS 连接过程:

    

                            

头晕,这个还是看官方的文档:https://developer.apple.com/library/prerelease/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

信鸽 使用流程:

1、配置真机调试证书, 推送测试证书 (度娘·······)

2、注册iOS推送

 float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if(sysVer < 8){
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}else{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:[NSSet setWithObject:categorys]];
[[UIApplication sharedApplication] registerUserNotificationSettings:userSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}

3、注册设备信息(deviceToken)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//注册设备
NSString * deviceTokenStr = [XGPush registerDevice: deviceToken]; //打印获取的deviceToken的字符串
NSLog(@"deviceTokenStr is %@",deviceTokenStr);
}

3、注册信鸽

[XGPush startApp:@“10086” appKey:@"key"];

//设置账户
[XGPush setAccount:@"123456"];

//推送统计 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
   [XGPush handleReceiveNotification:userInfo];
}

 

iOS(本地通知与远程通知)的更多相关文章

  1. ios开发——实用技术OC-Swift篇&本地通知与远程通知详解

    本地通知与远程通知详解 一:本地通知   Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...

  2. iOS10以前的本地通知和远程通知

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

  3. iOS 远程通知(Remote Notification)和本地通知(Local Notification)

    ios通知分为远程通知和本地通知,远程通知需要连接网络,本地通知是不需要的,不管用户是打开应用还是关闭应用,我们的通知都会发出,并被客户端收到 我们使用远程通知主要是随时更新最新的数据给用户,使用本地 ...

  4. iOS Notification – 远程通知

    本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...

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

    iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...

  6. iOS: 本地通知的前后变化(iOS10)

    一.介绍  通知和推送是应用程序中很重要的组成部分.本地通知可以为应用程序注册一些定时任务,例如闹钟.定时提醒等.远程推送则更强大,提供了一种通过服务端主动推送消息到客户端的方式,服务端可以更加灵活地 ...

  7. IOS本地通知

        发送通知: UILocalNotification *newNotification = [[UILocalNotification alloc] init]; if (newNotifica ...

  8. IOS本地通知:UILocalNotification使用记录

    第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...

  9. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

随机推荐

  1. 用python脚本通过excel生成文件夹树结构

    大概这样写标题是对的吧... 目标: 通过excel目录结构文档生成文件夹树结构. 也就是: 通过下面的excel

  2. oracle11g dataguard 完全手册

    一.前言: 网络上关于dataguard的配置文章很多,但是很多打着oracle11g的文章实际都是只能在9 10 上运行,比如FAL_CLIENT在11g中已经废弃,但是现在网络上的文章都是没有标注 ...

  3. Linux文件权限

    Permission deny 权限 拒绝   查看权限 ls -a ls -la expression 查看文件夹里边东西的权限   用户群的分类 组群:一个操作系统可能几个人同时用 方便小组的文件 ...

  4. 【解决】SharePoint 2013 with SP1安装问题及解决

    安装环境 操作系统:Windows Server 2008 R2 SP1 安装包:SharePoint Server 2013 with SP1 问题及解决 I. 必备组件(prerequisitei ...

  5. Shift的用法

    Shell编程中Shift的用法     位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的 ...

  6. .net 生成 静态页面

    .net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...

  7. OCP认证之Oracle的SQL语言基础(一)

    一.Oracle命令类别 数据操纵语言(DML):select;insert;delete;update;merge 数据定义语言(DDL):create;alter;drop;truncate 事物 ...

  8. Javaweb命名规则

    一.类命名命名规范:以大写字母开头,如果有多个单词,每个单词头字母大写.例如:StudentInfo 二.接口命名命名规范:以大写字母"I"开头,如果有多个单词,每个单词头字母大写 ...

  9. 进击的docker 二 : docker 快速入门

    1.安装docker 1.1.安装环境 [root@docker ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@docke ...

  10. java SE编写图形应用程序

    借鉴了java 核心技术卷1    并参考http://www.jb51.net/article/56158.htm 添加了JTextField的使用. ####################### ...