donglanying3855 2014-12-13 05:13
浏览 52
已采纳

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

I am referring this tutorial http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1.. when it run this code it show's Successfully delivered, but message not appearing on device. i did complete steps for APNS,

Here's whatever i tried is, where i am wrong?

PHP Code :

<?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 CODE

 - (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];

  }
}
  • 写回答

1条回答 默认 最新

  • dprntkxh703029 2014-12-23 13:06
    关注

    I got the solution and get the push notification.

    The problem was with creation of p12 key to pem file. I was using this command in terminal for converting file to pem from p12 :

    "openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12"

    which removes the certificates "-nocerts" so i used following command to do same operation :

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

    and then do as per this link and it will work perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决