iOS 中的Push Notifications简单实现(APNS)

时间:2021-02-02 02:30:03

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选项。

iOS 中的Push Notifications简单实现(APNS)

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

iOS 中的Push Notifications简单实现(APNS)

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

iOS 中的Push Notifications简单实现(APNS)

生成后的截图

iOS 中的Push Notifications简单实现(APNS)

以上就是需要申请的东西,之后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");

效果如图

iOS 中的Push Notifications简单实现(APNS)

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