APNS(3)Write the Easy Client App

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值