Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications。ios的这2种notification虽然最终的表现相同,都是给用户一个弹出了一条通知,但他们的目的和发出通知的方式完全不同。另外,还需要注意,iOS中还存在一个消息类NSNotification,这个类所指的“通知”和刚才提到的“通知”不同,它指程序逻辑之间的消息通知,是一种编程技术。

Push Notifications的详细内容,请参考Local and Push Notification Programming Guide中的Apple Push Notification Service。

以下是Push Notifications的简介

Each push notification includes a payload. The payload contains information about how the system should alert the user as well as any custom data you provide. The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit.

For each notification, compose a JSON dictionary object (as defined by RFC 4627). This dictionary must contain another dictionary identified by the key aps. The apsdictionary contains one or more properties that specify the following actions:

  • An alert message to display to the user

  • A number to badge the application icon with

  • A sound to play

在ios7中,有2个函数与远程通知有关,分别是application:didReceiveRemoteNotification: 和 application:didReceiveRemoteNotification:fetchCompletionHandler:

1. application:didReceiveRemoteNotification:

根据程序是否处于running状态,分为2中情况,注意这里的running状态包括 inactive, active, background,suspended 4种状态,即除了not running 之外的所有状态。

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action.

If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: orapplication:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.

我想,这样可能是由于,如果程序处于not running状态,那么它根本没有application delegete实例,也就不能响应那些高级的代理方法了。

If your delegate also implements the application:didReceiveRemoteNotification:fetchCompletionHandler: method, the app object calls that method instead of this one.

2. application:didReceiveRemoteNotification:fetchCompletionHandler:

这个函数是ios7才加入的,首先如果你实现了它,那么application:didReceiveRemoteNotification:就没用了。从这里可以看出,其实,这个函数是ios7中对application:didReceiveRemoteNotification:进行的一种扩充。使用这个函数必须启用程序的remote notifictions后台机能。这个函数的最大特点如下:

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. If the user opens your app from the system-displayed alert, the system calls this method again so that you know which notification the user selected.

注意这里的2次调用。按照文档说的,如果你的程序在not running状态,系统会先launch it into background,之后再执行这个函数。当用户点击了通知,还会再次调用这个方法。但是ios7.0.6存在bug,这个函数不会被launch it into background。它的作用现在看来同第一个函数一致!https://devforums.apple.com/thread/209664?tstart=0 这里是大家的讨论,需要开发者帐号。


下面就动手自己实现一个测试用的demo程序,实现push notificaiton

首先要有苹果开发者帐号,之后步奏如下

1 注册app id

使用push notification 必须使用明确的app id,并且该app id 应该选中Push Notifications选项。

2. 生成与该app ID相关的provisioning file

3.生成在push server端用的证书文件

生成后的截图

以上就是需要申请的东西,之后ios端需要实现2个函数,

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"deviceToken: %@", deviceToken);
} // Handle an actual notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"user info %@",userInfo); NSString *contentStr =[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; NSLog(@"content str is %@",contentStr); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:contentStr delegate:self cancelButtonTitle:@"good" otherButtonTitles:nil]; [alertView show]; }

ios端就完成了。

之后需要使用JavaPNS这个开源工程发送notification,发送中需要填写你获得的token 和 刚才申请的certificate,certificate要加工成p12文件。

Push.alert(pushStr, "ApnsTest.p12", "123456", false, "ca3d0687ea9ac22fb18b468fb40dc388180a5d33c4b7bda8746e64d23a041cf3");

效果如图

请注意,由于网络的问题,有时候反应是相当的慢,比如这次测试,5分钟后才收到通知,我还以为写错了。。。

iOS 中的Push Notifications简单实现(APNS)的更多相关文章

  1. IOS中的数据存储 简单总结

      1.  NSKeyedArchiver(加密形式)   2.  plist   3.  NSUserDefaults   4.  writeToFile    5.  SQLite3 ==== N ...

  2. iOS 中push和pop到底系统做了些什么事

    iOS中的push和pop是一个很常用的视图切换方法,他们是成对出现的, 简而言之,push就是压栈,pop就是出栈! [self.navigationController pushViewContr ...

  3. Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key

    Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 OCT 6, 2016 Push notifications are ...

  4. (转)pem, cer, p12 and the pains of iOS Push Notifications encryption

    转自:http://cloudfields.net/blog/ios-push-notifications-encryption/ The serious pains of setting up a ...

  5. 【IOS】ios中NSUserDefault与android中的SharedPreference用法简单对比

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3405308.html 有Android开发经验的朋友对Shar ...

  6. iOS中XMPP简单聊天实现 好友和聊天

    版权声明本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;微信公众号:陈怀哲(chenhuaizhe2016) ...

  7. 转载 -- iOS中SDK的简单封装与使用

    一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...

  8. iOS中动画的简单使用

    iOS中的动画右两大类1.UIView的视图动画2.Layer的动画 UIView的动画也是基于Layer的动画动画的代码格式都很固定 1.UIView动画 一般方式[UIView beginAnim ...

  9. iOS 中多线程的简单使用

    iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...

随机推荐

  1. 将定时任务cron 解析成中文

    在使用定时器 quartz 时,其中的cron 表达式,老板表示作为开发的你能看懂外,其他的非开发同事可能看不懂,要用一个他们能看懂的方式表达出来. 还好我们的项目要求的表达式不是特别的麻烦,所以就写 ...

  2. 【UVA 1586】Ancient Cipher

    题 题意 给你一个只含CHON的有机物的化学式如C6H5OH求相对分子质量 分析 ... 代码 switch #include<cstdio> #include<cctype> ...

  3. BZOJ4241 历史研究

    Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件. ...

  4. codevs1358 棋盘游戏

    题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每次棋子可以往右方或下方的相邻格子移动,求一条经 ...

  5. 活用scanf

    scanf()是C语言中用于读入格式化数据(formatted data)的函数. 我们可能对scanf()的一般用法已经了然,而至于scanf()读入数据的机制恐怕并不十分清楚. 下面我将比较详细地 ...

  6. HD 2177(威佐夫博弈 入门)

    取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. CentOS 配置hadoop

    Hadoop是用作处理大数据用的,核心是HDFS.Map/Reduce.虽然目前工作中不需要使用这个,但是,技多不压身,经过虚拟机很多遍的尝试,终于将Hadoop2.5.2的环境顺利搭建起来了.    ...

  8. MyEclipse修改项目名称后,部署到 tomcat问题

    问题描述: 修改项目名称后,部署到tomcat问题 解决方案: 项目->属性->myelcipse->web下,修 改web context root就可! 要在eclipse里面改 ...

  9. Centos目录结构详细版

    使用linux也有一年多时间了  最近也是一直在维护网站系统主机  下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...

  10. Hibernate之多对多

    一.项目结构如下图 二.保存学生和课程及其学生选课关系代码如下(测试类中不能再有双向关联,否则会报错,因为,都维护了中间表外键,会有中间表外键冲突,如果非要写双向关联,就需要配置中设置某一方维护主键, ...