- 浏览: 2539735 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
APNS(3)Write the Easy Client App
A Very Basic App
Create a new project in Xcode and pick View-based Application.
Product Name: EasyiOSExample
Company Identifier: com.somecompany
Device Family: iPhone
For my mistake here, I need to change the Bundle Identify to com.somecompany.easyiosexample to make it work
Error Message:
Property 'viewController' not found on object of type '*AppDelegate *'
Solution:
Uncheck the story-board during creating the project.
Place these codes in easyiosexampleAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.windowmakeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplicationsharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
returnYES;
}
When I run that on my real device, it will ask me
"*" Would Like to Send You Push Notifications
Notifications may include alerts, sounds and icon badges. These can be configured in Settings.
I choose 'OK'.
And after that, I can configure that in Settings -> Notification
Add some codes to watch the device token which is getting back from the APNS.
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
These lines will print out the device token when we start our app on mobile device.
Actually, if I am not new to iOS, I will write codes here to connect to a REST JSON service or WS service to report the device token to a third part server. The third part server need to know the device token when it wants to send push notification to my device.
Demo a Simple php Server Side
find the file simplepush.php. It is in the http://d1xzuxjlafny7l.cloudfront.net/downloads/SimplePush.zip.
Change the file ck.pem which I generate to the working directory of SimplePush.
Change the first 3 lines of simple push.php
// Put your device token here (without spaces):
$deviceToken = 'token from the console when we start the iOS app';
// Put your private key's passphrase here:
$passphrase = 'my CERT key';
// Put your alert message here:
$message = 'The content I want to send to notification.';
I am not good at PHP, but the codes are really simple, just open SSL connection to APNS and post a JSON string to it.
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Good to know the APNS, I am green hand to iOS. I need more practices.
References:
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2
http://www.raywenderlich.com/tutorials
A Very Basic App
Create a new project in Xcode and pick View-based Application.
Product Name: EasyiOSExample
Company Identifier: com.somecompany
Device Family: iPhone
For my mistake here, I need to change the Bundle Identify to com.somecompany.easyiosexample to make it work
Error Message:
Property 'viewController' not found on object of type '*AppDelegate *'
Solution:
Uncheck the story-board during creating the project.
Place these codes in easyiosexampleAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.windowmakeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplicationsharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
returnYES;
}
When I run that on my real device, it will ask me
"*" Would Like to Send You Push Notifications
Notifications may include alerts, sounds and icon badges. These can be configured in Settings.
I choose 'OK'.
And after that, I can configure that in Settings -> Notification
Add some codes to watch the device token which is getting back from the APNS.
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
These lines will print out the device token when we start our app on mobile device.
Actually, if I am not new to iOS, I will write codes here to connect to a REST JSON service or WS service to report the device token to a third part server. The third part server need to know the device token when it wants to send push notification to my device.
Demo a Simple php Server Side
find the file simplepush.php. It is in the http://d1xzuxjlafny7l.cloudfront.net/downloads/SimplePush.zip.
Change the file ck.pem which I generate to the working directory of SimplePush.
Change the first 3 lines of simple push.php
// Put your device token here (without spaces):
$deviceToken = 'token from the console when we start the iOS app';
// Put your private key's passphrase here:
$passphrase = 'my CERT key';
// Put your alert message here:
$message = 'The content I want to send to notification.';
I am not good at PHP, but the codes are really simple, just open SSL connection to APNS and post a JSON string to it.
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Good to know the APNS, I am green hand to iOS. I need more practices.
References:
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2
http://www.raywenderlich.com/tutorials
发表评论
-
ionic UI(5)UI and Backend
2016-12-02 03:22 589ionic UI(5)UI and Backend 1 Pr ... -
Stanford Cource(2)Demo App Caculator
2014-06-24 01:29 897Stanford Cource(2)Demo App Ca ... -
Mono on MAC
2014-06-04 03:27 974Mono on MACJust fine the tool f ... -
IOS7 App Development Essentials(4)IPhone5, IPhone5s, IPhone5c
2014-04-11 03:59 981IOS7 App Development Essentia ... -
IOS7 App Development Essentials(3)NSUserDefaults
2014-04-11 02:58 1005IOS7 App Development Essentia ... -
IPhone and Location(2)Documents Region Monitoring and Region Sample
2013-10-18 05:10 1725IPhone and Location(2)Documents ... -
IPhone and Location(1)Documents User Location
2013-10-18 03:50 1303IPhone and Location(1)Documents ... -
Learn Objective C(6)Programming with Objective-C - Working with Blocks and Deali
2013-10-18 00:02 924Learn Objective C(6)Programming ... -
Learn Objective C(5)Programming with Objective-C - Working with Protocols and Va
2013-10-17 23:47 996Learn Objective C(5)Programming ... -
Learn Objective C(4)Programming with Objective-C - Encapsulating Data and Custom
2013-10-17 23:23 934Learn Objective C(4)Programming ... -
Learn Objective C(3)Programming with Objective-C - Defining Classes, Working wit
2013-10-17 23:09 1013Learn Objective C(3)Programmi ... -
Learn Objective C(2)Learn Objective-C in Day 6 - 4 ~ 6
2013-10-17 00:30 960Learn Objective C(2)Learn Obj ... -
Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
2013-10-17 00:22 1109Learn Object C(1) Learn Objec ... -
APNS(4)Recall the Process and Learn Java APNS
2013-04-18 02:48 3466APNS(4)Recall the Process and L ... -
Build the iOS Things with J2Objc
2013-04-12 03:25 2462Build the iOS Things with J2Obj ... -
APNS(2)Try to Finish the first Example
2013-01-14 07:56 1537APNS(2)Try to Finish the first ... -
Stanford Cource(1)MVC and Object-C
2012-12-14 14:04 1316Stanford Cource(1)MVC and Objec ... -
Some VI Tips
2012-11-15 04:48 1092Some VI Tips Today, I need to c ... -
MAC Mini Setup
2012-09-25 18:45 1325MAC Mini Setup I am dealing wit ... -
Android Talker(1)MAC Environment
2012-09-01 00:16 1921Android Talker(1)MAC Environmen ...
相关推荐
这是一款为iOS、Mac app提供推送测试的小工具。 您可以使用该工具为您的App发送Apple提供的推送信息,您可以使用便利构造器构造简易Payload,也可以使用原始方法手动写入Json字典来构造Payload。 为了发送推送,您...
easyapns, 使用 PHP & MySQL的简单 APNs 不再维护这里项目:请注意:EasyAPNs适用于精通 PHP,MySQL和 Objective C的程序员。 如果你不精通这些编程语言,请使用其他服务,如城市飞艇( http://urbanairship.com ) 。...
**Easy APNs Provider - iOS:Mac 推送测试工具** 在iOS应用开发中,Apple Push Notification service (APNs) 是苹果公司提供的一项服务,用于向iOS、watchOS、tvOS和macOS设备发送推送通知。APNs允许开发者通过...
资源分类:Python库 所属语言:Python 资源全名:apns-client-0.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
在Java开发环境中,当需要构建一个能够向Apple Push Notification Service (APNS)发送推送通知的服务时,`notnoop-java-apns`是一个常用的第三方库。这个压缩包`notnoop-java-apns.zip`包含了开发者用于实现这一功能...
为了简化开发流程,还可以考虑使用第三方库,如`Nito.PushNotification`或`EasyAPNS`,它们已经实现了上述步骤,并提供了更友好的API。 在项目压缩包中的"APNS"文件可能包含了实现上述功能的源代码、配置文件、示例...
1. **库的选择**:在C#中,有多个开源库可以帮助我们轻松实现APNS,如`EasyAPNS`、`Prowlsharp`等。它们封装了与APNs通信的细节,让开发者可以专注于消息内容的处理。 2. **设置推送服务器**:根据是生产环境还是...
3. **推送认证**:为了安全起见,开发者需要设置APNs证书,这涉及到在苹果开发者门户中创建并下载两个证书:生产环境和开发环境。每个证书对应不同的用途,前者用于上线应用的推送,后者用于测试和调试。 4. **推送...
3. **.NET APNS推送实现**: 在.NET环境中实现APNS推送,通常需要以下步骤: - **证书准备**:首先,开发者需要在Apple Developer Portal上生成推送服务的SSL证书,并将其下载为.p12文件。 - **连接到APNS**:使用...
APNSPHP是一个专门为苹果推送通知服务(Apple Push Notification service,简称APNs)设计的PHP库。这个库的主要目的是帮助PHP开发者方便地与苹果服务器进行通信,以便向iOS、macOS和watchOS设备发送推送通知。从...
Java整合APNS推送服务是将Java应用程序与Apple Push Notification Service(APNS)相结合,以便能够向iOS和tvOS设备发送即时消息。APNS是苹果公司提供的一个服务,它允许开发者在用户不打开应用的情况下,向他们的...
3. **构建APNS推送服务器** - 使用C#创建一个Windows服务或Web应用,作为推送服务器。 - 首先,需要获取APNS证书,这可以在Apple Developer Portal上完成。下载.p12文件,包含私钥和证书,用于身份验证。 - 引入...
3. **构建推送消息**:APNs推送消息包含一个设备Token(用户设备的唯一标识)、一个通知payload(JSON格式,包含标题、内容等信息)以及可选的优先级和有效期。 4. **发送推送**:通过已建立的连接,将构建好的推送...
3. SSL证书文件(.cer):用于标识服务端。 在创建证书之前,开发者需要登录到iPhone Developer Connection Portal(***),点击“App IDs”选项,创建一个不使用通配符的App ID,因为通配符ID无法用于推送通知服务...
apns4j 是 Apple Push Notification Service 的 Java 实现!Maven: <groupId>com.github.teaey</groupId> <artifactId>apns4j <version>1.0.1 示例代码:KeyStoreWraper keyStore = ...
3. **PHP连接APNs**: 使用PHP连接APNs通常涉及创建一个TCP套接字连接。你可以使用PHP的socket扩展来实现。连接时,需要提供正确的服务器地址(根据生产环境或测试环境选择不同的地址)、端口以及之前创建的.pem...
apns.jar包,用于apns推送,直接导入就可以使用了
3. **建立SSL连接**:使用SSL/TLS协议连接到APNS服务器。这通常需要使用SSL证书,并通过SSLStream或其他相关的网络库来实现。对于Python,可以使用pyOpenSSL或ssl模块;对于Node.js,有`tls`模块可用。 4. **构建推...
1. **创建APNs证书**:在苹果开发者中心,为你的App ID创建一个APNs证书,区分开发环境和生产环境。下载并双击安装到Keychain Access。 2. **构建推送消息**:推送消息包含头信息(如设备Token、通知类型、优先级等...
3. **构建推送通知**:APNs通知由JSON对象组成,包含设备令牌(Device Token)、通知类型、标题、正文等字段。必须正确设置这些字段,以确保消息能够成功送达。 以下是使用Java进行APNs推送的基本步骤: 1. **加载...