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. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  2. Fence 设备

    RHCS中必须有Fence设备,在设备为知故障发生时,Fence负责让占有浮动资源的设备与集群断开. REDHAT的fence device有两种, 内部fence设备: IBM RSAII卡,HP的 ...

  3. javaScript基础练习题-下拉框制作

    1.基础回顾 如何让一个段javascript在文档加载后执行,(因为自己忘了,所以顺便复习一下) window.onload = function(){}; <!DOCTYPE html PU ...

  4. HDU 2896 病毒侵袭

    Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福 ...

  5. java连接mysql(二)

    模拟转账成功时的业务场景 import java.sql.*; public class TransactionDemo1 { public static void main(String[] arg ...

  6. ORACLE RAC集群的体系结构

    RAC是一个完整的集群应用环境,它不仅实现了集群的功能,而且提供了运行在集群之上的应用程序,即Oracle数据库.无论与普通的集群相比,还是与普通的Oracle数据库相比,RAC都有一些独特之处. R ...

  7. [Python] Python 之 __new__() 方法与实例化

    __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在 Python 中存在于类里面的构造方法 __init__() 负责将类的实例化,而在 __init__() ...

  8. java变量作用域

      1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可 ...

  9. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  10. ecshop后台admin路径怎么修改

    ecshop后台admin路径怎么修改 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-03-25   ecshop如何修改后台admin路径? 大家都知道ec ...