推送通知iphone(我的设备未收到通知)

时间:2011-10-03 11:16:46

标签: php ios iphone push-notification apple-push-notifications

我遇到的问题是我的iOS设备没有收到任何推送通知。

目标C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (
        UIRemoteNotificationTypeAlert |
        UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeSound
      )
     ];

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(str);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    UIAlertView *dataAlert = [[UIAlertView alloc] initWithTitle:@"Device Token"
                                                        message:@"data"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [dataAlert show];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"APN device token: %@", deviceToken);
    NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken];
    UIAlertView *deviceTokenAlert = [[UIAlertView alloc] initWithTitle:@"Device Token"
                                                               message:deviceTokenString
                                                              delegate:self
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
    [deviceTokenAlert show];
}

PHP

<?php
$deviceToken = "a448b8946a5de3801dc6a11862a5a0bf11f1adc16xxxxxxxxxxxx"; // masked for security reason
// Passphrase for the private key
$pass = 'molik';

// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'Test Message';
//$badge = (int)$_GET['badge'] or $badge = (int)$argv[2] or $badge  = 1;
$sound = $_GET['sound'] or $sound = $argv[3] or $sound  = 'default';

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $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);

// for production change the server to ssl://gateway.push.apple.com:219
$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 $errstr\n";
    return;
}
else {
    print "Connection OK\n";
}

$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', $deviceToken) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";



$result=fwrite($fp, $msg);

echo $result;
fclose($fp);
?>

运行PHP代码时会显示输出:

Connection OK sending message :{"aps":{"alert":"Test Message","sound":"default"}}

但在设备方面,没有收到任何通知。

2 个答案:

答案 0 :(得分:2)

我遇到了很多与公钥和私钥有关的问题。

最后我撤销了所有证书,并按照以下链接,然后我在设备中获得推送通知。

您还可以在下面的link找到php的示例代码。

答案 1 :(得分:0)

证书问题。尝试创建新的应用程序ID并生成development.cer and production.cer`。之后生成配置文件。