IOS PUSH 实践操作~~~~
1.推送过程简介
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString* dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSLog(@"deviceToken:%@", dt);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"didReceiveRemoteNotification~~~~");
NSLog(@"remote notification: %@",[userInfo description]);
NSString* alertStr = nil;
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSObject *alert = [apsInfo objectForKey:@"alert"];
if ([alert isKindOfClass:[NSString class]])
{
alertStr = (NSString*)alert;
}
else if ([alert isKindOfClass:[NSDictionary class]])
{
NSDictionary* alertDict = (NSDictionary*)alert;
alertStr = [alertDict objectForKey:@"body"];
}
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
if ([application applicationState] == UIApplicationStateActive && alertStr != nil)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pushed222 Message" message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
<?php
// Put your device token here (without spaces):
$deviceToken = \'7d40b88~~~~~80013f376603ff7a0237509336d6616ae04564b3412b86\';
// Put your private key\'s passphrase here:
$passphrase = \'198~~~~30\';
// Put your alert message here:
$message = \'My first push notification!\';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, \'ssl\', \'local_cert\', \'apns-dev.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);