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. 【UVA 11401】Triangle Counting

    题 题意 求1到n长度的n根棍子(3≤n≤1000000)能组成多少不同三角形. 分析 我看大家的递推公式都是 a[i]=a[i-1]+ ((i-1)*(i-2)/2-(i-1)/2)/2; 以i 为 ...

  2. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

  3. a[1000][1000]程序崩溃

    1000 * 1000是大于65536的.如果不是需求需要,没必要开辟如此之多的空间.因为这些空间实在栈上申请的(如果是局部变量),栈的空间是有限的并且是宝贵的,所以呢,开辟太多的空间而不适用很可能会 ...

  4. 在myeclipse10.7.1中写代码有很多红x

    代码没问题,但是很多代码前都有红x.在doc中执行都没有问题 jdk版本不对应    //第一步:菜单栏Window--Preferences--Java--Installed JREs--右边Add ...

  5. js中的全选,不选,和反选按钮的设定

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 转 vagrant package[打包命令]详解

    转 vagrant package[打包命令]详解   vagrant的一个非常重要的功能就是在你的同事之间分享你的box从而使大家的开发环境保持同步,打包[package]正是实现这一功能的关键所在 ...

  7. ci控制器写规范

    不需要后缀名 文件名全部小写 所有控制器需要直接或者间接继承CI_Controller 以下划线开头的方法为私有方法,不能被请求 protected private的方法不能被浏览器请求 ci方法名不 ...

  8. 初学structs2,简单配置

    一.structs2-demo1项目下新建structs.xml文件,文件名必须是structs 二.package节点配置及其子节点配置 <!--name:单纯给包起个名字,保证和其他包不重名 ...

  9. serialVersionUID要注意以下几点:

    今天在使用eclipse开发的时候,遇到一个warning,看到warning我总觉得不爽,使用自动修复后,发现eclipse在代码中加入了“private static final long ser ...

  10. apache AddDefaultCharset

    AddDefaultCharset 指令 说明 当应答内容是text/plain或text/html时,在HTTP应答头中加入的默认字符集 语法 AddDefaultCharset On|Off|ch ...