Apple Push Notification Service Tutorial
http://ameyashetti.wordpress.com/2009/07/31/apple-push-notification-service-tutorial/
Apple Push Notification Service (APNS) an service user by apple to notify application. The notification can be of various Text Alert with or without sound, Baggage number update on icon etc.
Below are the steps to construct an simple application that receives notification form APNS.
The steps are for development testing on sandbox APNS service from Apple
Getting Ready with Certificate and key.
1. Generate a certificate signing request from your Mac’s keychain and save to disk.(Steps same as creating certificate for development but don’t submit not).Pleas store this certificate in a safe location as it might be re-required to invoke APNS.
2. Login into your development account and visit iPhone Developer Program Portal.
3. Click App IDs on left. Create an new id without wild charter (com.mydomain.applicationName). This name will be used while setting up your application to be signed with development certificate. If you use wild character like ‘*’ the iPhone Developer Program Portal will not allow the App ID to be used for notification.
4. After submitting the new App ID you will be guided to the list page. Click configure to edit setting. Check ‘Enable for Apple Push Notification service’ to enable APNS , and click Configure next to ‘Development Push SSL Certificate’.
5. Upload your request certificate generated in step 1 and download the certificate (aps_developer_identity.cer) from the Program Portal. Double click on this certificate to save it your key chain. Export this key by clicking on this newly installed certificate. The exported key is saved as Certificate.p12 file on your system. Please store it other files uploaded or downloaded from portal program. This .p12 file is used in later steps for signing your Provider server.
6. Click on Provisioning in left bar and create a new provisioning profile. Use the new created App Id and select the device you want to use for development. Download the new provisioning profile save with other files. Close XCode if open and drag drop new provisioning profile on your XCode in your Doc bar.Your ready with certificate and key.
Getting ready with the application
* Create an simple view based application.
* Paste these line of code in your application.
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here….
[window addSubview:viewController.view];
[self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//NSLog(@”devToken=%@”,deviceToken);
[self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:@”Ok” otherButtonTitle:@”"];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@”Error in registration. Error: %@”, err);
[self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err] cancleButtonTitle:@”Ok” otherButtonTitle:@”"];
}
-(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle{
UIAlertView *alert;
if([otherTitle isEqualToString:@""])
alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
else
alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
[alert show];
[alert release];
}
1. Please be careful with the error reporting part in the code as error tell a lot about the development state.
2. Right click on the application in target in the left and click Get Info to configure the application. Click on property tab and paste your App ID in the identifier text-field.
3. Click build tab, select debug and select your new provisioning profile.
4. Click Device 3.0 and Build Go to distribute binary to your connected device.
5. Starting the application it should first alert and message that it registering for notification. Followed by and alert the application is registering for notification allow or deny. Followed by an alert that displays the device token ID. Note this token as this will be used by your server code to communicate with your device. If the second message is error then either something has gone wrong with the certificate and your application cannot register your certificate start over again.
Getting ready with Notification service provider.
Download an stand alone MAC application (PushMeBaby http://stefan.hafeneger.name/download/PushMeBabySource.zip) to test. There are two modification in the application to get started.
Place an copy of the aps_developer_identity.p12 file in the application folder. Import the file in the application by right clicking and Add > Existing File.
Set the following in the application’s delegate file as shown below
self.deviceToken = @”XXXXX XXXXX XXXXX XXXXXX XXXXXX”;
//First your device id token.
self.payload = @”{\”aps\” : { \”alert\” : \”You got your emails.\”,\”badge\” : 9,\”sound\” : \”bingbong.aiff\”},\”acme1\” : \”bar\”,\”acme2\” : 42}”;
//The pay load
self.certificate = [[NSBundle mainBundle] pathForResource:@”aps_developer_identity” ofType:@”cer”]; //The certificate file.
* Don’t depend on the application’s text-field as it doesn’t work.
* Start the application and you will get a message the application is trying to access your key , click ‘Allow’. Click Push in the application window. Wait for a 20 seconds and you should immediately get an notification on your iPhone / iTouch.
Getting ready with test code for actual provider sever (PHP).
* For server on linux environment you will require different kind of certificate. Following are the steps to create it. Use MAC console to fire the following commands.
openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificate.p12
provide new password if asked.
openssl pkcs12 -nocerts -out key.pem -in Certificate.p12
provide new password if asked.
cat cert.pem key.unencrypted.pem > ck.pem
Create an PHP file provide.php
$message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, ’ssl’, ‘local_cert’, ‘ck.pem’);
// assume the private key passphase was removed.
// stream_context_set_option($ctx, ’ssl’, ‘passphrase’, $pass);
$fp = stream_socket_client(’ssl://gateway.sandbox.push.apple.com:2195′, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print “Failed to connect $err $errstrn”;
return;
}
else {
print “Connection OKn”;
}
$payload = json_encode($body);
$msg = chr(0) . pack(”n”,32) . pack(’H*’, str_replace(’ ‘, ”, $deviceToken)) . pack(”n”,strlen($payload)) . $payload;
print “sending message :” . $payload . “n”;
fwrite($fp, $msg);
fclose($fp);
?>
Note : Your PHP server must have json_encode support.
Run your file on linux console as ‘php provide.php’
分享到:
相关推荐
### Apple Push Notification Service (APNS) 简介 #### 一、APNS 概述 Apple Push Notification Service(简称 APNS)是苹果公司提供的一种推送通知服务,它允许开发者向用户设备上的应用发送实时更新的信息。...
在Android平台上,实现即时通知通常会借助Google的云消息推送服务(Google Cloud Messaging,简称GCM),但在iOS系统中,对应的则是Apple Push Notification service(APNs)。然而,这里提到的"android apns"可能是...
围绕 Apple Push Notification Service 和 Google Cloud Messaging Service 的简单 HTTP 包装器 安装 安装 Node.js(0.10.x,不是 0.11.x) 安装Redis 克隆源 运行npm install 运行 Pushlet $ 节点pushlet.js ...
首先,我们需要明白APNS(Apache Push Notification Service)并不是Android系统的官方服务,而是通常指苹果的Push Notification Service。然而,在这里,“apns”标签可能是开发者为了表示类似功能的一个简称,或者...
用于与Apple Push Notification Service(APN)交互的Python库 安装 从GitHub下载源代码或使用easy_install: $ easy_install apns 样品用法 import time from apns import APNs , Frame , Payload apns = APNs ( ...
这是一个用于与Apple Push Notification Service(APNS)进行交互的小型C ++ 11项目。 您可以将推送通知发送到iOS设备或Safari浏览器(仅适用于OS X 10.9)。 它还处理反馈服务。 安装 首先在global.hpp中设置证书...
Erlang库,用于使用Apple Push Notification Service。 。 。 。 如何使用 在您的rebar.config中设置依赖项: {deps, [ {dp_push, ".*", {git, "https://github.com/yzh44yzh/dp-push.git", "v1.0"}} ]}. ...
apns Go程序包可与Apple Push Notification Service交互特征该库实现了一些我们在其他任何一个库中都找不到的功能: 客户端寿命长-Apple的文档说,您应该保持而不是为每个有效负载创建新的连接新协议的使用-Apple...
Laravel 提供了一个方便的工具来处理这一过程,特别是与 Apple Push Notification Service (APNS) 和 Google Cloud Messaging (GCM) 集成。 首先,我们需要安装 "laravel-push-notification" 扩展包。在 Laravel ...
PyAPNs2 用于通过HTTP / 2协议与Apple Push Notification Service(APN)进行交互的Python库安装从GitHub下载源代码或使用pip: $ pip install apns2样品用法 from apns2 . client import APNsClientfrom apns2 . ...
无间隙具有连接池的独立Apple Push Notification Service 该软件包内置到一个独立的应用程序中,该应用程序侦听服务器(使用软件包)以推送任何消息。 Gapless使用池来与Apple推送通知服务器建立多个连接。 这样可以...
6. 在你的App ID下面找到Configure,选中,勾选“Enable for Apple Push Notification service”,然后点击Development Push SSL Certificate下面的Configure。 7. 点击Continue继续,然后点击Choose File,选中刚...
推杆OS X和iOS应用程序和框架可与Apple Push Notification Service(APN)一起使用安装使用安装Mac应用程序: brew cask install pusher 或下载最新的Pusher.app二进制文件:另外,您可以使用将包括: pod 'NWPusher...
n Apple推送通知服务(APN)的调试应用程序。特征轻松地将推送通知发送到APNS(Apple推送通知服务)(根本不需要配置) 加载/保存文档,包括令牌和JSON负载直接从钥匙串中获取证书自动获取设备令牌; 无需再通过日志...
在Android平台上,推送通知(Push Notification)是一种高效且节省资源的方式,用于向用户发送应用程序相关的实时信息,即使应用并未在前台运行。这个“android push notification 下载即可运行”项目表明,它提供了...
本文将详细介绍如何实现Android Push Notification,包括服务器端(Server)和客户端(Client)的设置以及操作配置流程。 1. 服务器端(Server)实现 服务器端负责发送推送通知到Google的云消息服务(GCM,现在...
Laravel Push Notification是Laravel框架的一个扩展包,它允许开发者轻松地向移动设备(如Apple Push Notifications (APN)、Google Cloud Messaging (GCM)、Windows Push Notification (WPN)以及BlackBerry设备)...
在iOS开发中,Push Notification Service(简称PNS)是一种苹果提供的服务,允许应用程序在后台接收来自服务器的消息。本文将深入探讨iOS中的简单Push Service代码,包括其工作原理、实现方式以及如何集成到你的应用...
Apple Push Notification Service (APNs) 是苹果公司提供的一项服务,允许开发者向运行iOS、iPadOS、watchOS、macOS以及tvOS的应用程序发送实时的通知。在这个场景中,我们讨论的是一个名为“pu.sh”的Bash脚本,它...
在iOS应用开发过程中,开发者可能遇到一种常见的错误提示——"Missing Push Notification Entitlement",这意味着在你的应用中包含了用于Apple Push Notification service (APNs) 的API,但是缺少了必要的权限设置,...