2014-12-13 80 views
2

我指的是本教程。 http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 ..當它運行此代碼時,它顯示已成功發送,但消息未顯示在設備上。我做了完整的APNS步驟,推送通知消息未出現在設備中

這裏是我嘗試的是,我錯了?由於提前

PHP代碼:

<?php 

// Put your device token here (without spaces): 
$deviceToken = 'Device Token'; 

// Put your private key's passphrase here: 
$passphrase = 'pushchat'; 

// Put your alert message here: 
$message = 'My first push notification!'; 

//////////////////////////////////////////////////////////////////////////////// 

$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); 

的iOS代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    { 
    // iOS 8 Notifications 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

    [application registerForRemoteNotifications]; 
    } 
    else 
    { 
    // iOS < 8 Notifications 
    [application registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; 
    } 


    //other code 

    return YES; 
    } 

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
    { 
    // NSString *DeviceTokenString = [NSString stringWithFormat:@"%@",deviceToken]; 
    // NSLog(DeviceTokenString); 
    NSString *devicePushToken=[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] ; 
    devicePushToken = [devicePushToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"%@", [NSString stringWithFormat:@"%@", devicePushToken]); 
    } 

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
    if (err.code == 3010) { 
     NSLog(@"Push notifications are not supported in the iOS Simulator."); 
    } else { 
     // show some alert or otherwise handle the failure to register. 
      NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", err); 
    } 
} 

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
     NSLog(@"%@", userInfo); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message: 
         [userInfo objectForKey:@"inAppMessage"] delegate:nil cancelButtonTitle: 
         @"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 

    UIApplicationState state = [application applicationState]; 

    // If your app is running 
    if (state == UIApplicationStateActive) 
    { 

    //You need to customize your alert by yourself for this situation. For ex, 
    NSString *cancelTitle = @"Close"; 
    NSString *showTitle = @"Demo Push Notification"; 
    NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" 
                 message:message 
                 delegate:self 
               cancelButtonTitle:cancelTitle 
               otherButtonTitles:showTitle, nil]; 
    [alertView show]; 

    } 
} 
+0

不知道關於php,但如果你真的想要簡單的解決方案推動你可以使用(解析)[www.parse.com] – 2014-12-13 06:44:56

+0

okey謝謝你 – 2014-12-13 06:47:26

+0

證書可能是問題。你確定ck.pem是從你的開發證書生成的嗎?嘗試[this](http://stackoverflow.com/questions/21250510/generate-pem-file-used-to-setup-apple-push-notification)方法來生成PEM。 – RoHaN 2014-12-13 06:48:15

回答

11

我得到了解決,並得到推送通知。

問題在於創建pem文件的p12鍵。 我用在終端此命令用於將文件從P12到PEM:

「OpenSSL的PKCS12 -nocerts -out PushChatKey.pem -in PushChatKey.p12」

其去除證書 「-nocerts」所以我用下面的命令來做同樣的操作:

openssl pkcs12 -in PKey.p12 -out PCKey.pem -nodes;

然後根據這個link做,它會完美的工作。

+0

真棒解決方案爲此,我認爲你必須對該教程給予評論,所以其他人都使用這個解決方案 – 2014-12-24 03:07:27

+0

yeap肯定今天我也給這個鏈接的解決方案:) – Ryuk 2014-12-24 04:09:02

+0

似乎沒有爲我工作 - 同樣的問題。消息已發送,但未發送 – FooBar 2015-01-03 18:16:30