php - 推送通知消息未出现在设备中

标签 php ios objective-c apple-push-notifications

我指的是本教程 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];

  }
}

最佳答案

我得到了解决方案并收到了推送通知。

问题在于创建 pem 文件的 p12 key 。 我在终端中使用此命令将文件从 p12 转换为 pem:

“openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12”

它删除了证书“-nocerts”,所以我使用以下命令来执行相同的操作:

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

然后按照这个link做它将完美运行。

关于php - 推送通知消息未出现在设备中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455780/

相关文章:

javascript - 使用 PHP 和 JQuery 的登录表单验证来自 MySQL 数据库的登录信息

php - 尽管一切都正确,但 MySql 查询语法错误

ios - 什么是 ABMultiValueAddValueAndLabel outIdentifier?

ios - 在 Objective C 中读取 JSON 文件

objective-c - 一种编程语言如何既是静态类型又是动态类型的?

php - Youtube I.D 解析新的 URL 格式

php - 如何创建本地 LRS 并使用 PHP 与 Tin can Api 集成以在我的 SCORM 包上运行

ios - intrinsicContentSize 与 sizeThatFits。有什么不同?每个的用例是什么?

ios - 如何通过 iOS 8 中提供的新 API 在 iPhone 6 上使用气压计?

objective-c - 在 SplitViewController 中弹出一个 ViewController