iOS 中的Push Notifications简单实现(APNS)
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)的更多相关文章
- IOS中的数据存储 简单总结
1. NSKeyedArchiver(加密形式) 2. plist 3. NSUserDefaults 4. writeToFile 5. SQLite3 ==== N ...
- iOS 中push和pop到底系统做了些什么事
iOS中的push和pop是一个很常用的视图切换方法,他们是成对出现的, 简而言之,push就是压栈,pop就是出栈! [self.navigationController pushViewContr ...
- 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 ...
- (转)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 ...
- 【IOS】ios中NSUserDefault与android中的SharedPreference用法简单对比
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3405308.html 有Android开发经验的朋友对Shar ...
- iOS中XMPP简单聊天实现 好友和聊天
版权声明本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;微信公众号:陈怀哲(chenhuaizhe2016) ...
- 转载 -- iOS中SDK的简单封装与使用
一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...
- iOS中动画的简单使用
iOS中的动画右两大类1.UIView的视图动画2.Layer的动画 UIView的动画也是基于Layer的动画动画的代码格式都很固定 1.UIView动画 一般方式[UIView beginAnim ...
- iOS 中多线程的简单使用
iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...
随机推荐
- Html-Css-li标签增加图片
制作一个需要显示颜色的圆点. <style> .item1 ul{list-style:none} .item1 li{padding-left:20px;background:url(圆 ...
- struts2中怎么把action中的值传递到jsp页面
对于如何把struts2的action中的值传到jsp页面中,主要的方法有2种: 使用转发视图利用request域中储存所需的值 使用重定向时存储数据进入session使其在jsp中可以获得 下面,让 ...
- 【CodeForces 624D】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- C++中getline被跳过
#include "stdafx.h" #include"iostream" #include"math.h" #include" ...
- 比RBAC更好的权限认证方式(Auth类认证)
Auth 类已经在ThinkPHP代码仓库中存在很久了,但是因为一直没有出过它的教程, 很少人知道它, 它其实比RBAC更方便 . RBAC是按节点进行认证的,如果要控制比节点更细的权限就有点困难了, ...
- Hibernate配置问题
Hibernate是对持久化对象操作,生成SQL语句达到操作数据库目的. 1.Hibernate可以通过两种方式来配置 (1).hibernate.cfg.xml,在此文件里hibernate-con ...
- MACD、BOLL、KDJ 三大组合精准把握趋势与买卖!
先看示意图,下图是布林线的3个轨道,其他都是股价走势 图1 股价,在布林线上轨.下轨之间运作.准确说,这话是不符合逻辑的,不是先有的轨道,然后股价再按照轨道运动.因为轨道是跟股价同时变化的.但是,股价 ...
- VS2010+OpenCV2.4.6永久性配置方法
1. 配置OpenCV环境变量 计算机->(右键)属性,出现如图1所示界面 单击“高级系统设置”,选中高级(标签)出现如图2所示界面 单击右下方的“环境变量”,弹出如图3所示界面,注意这里最好用 ...
- struts2 + ajax + json的结合使用,实例讲解
struts2用response怎么将json值返回到页面javascript解析,这里介绍一个struts2与json整合后包的用法. 1.准备工作 ①ajax使用Jquery:jquery-1.4 ...
- IE input file隐藏不能上传文件解决方法
当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...