简体   繁体   English

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

[英]Push notification message is not Appearing in Device

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. 我指的是这个教程http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 ..当它运行这段代码时它显示已成功交付,但是消息没有出现在设备上。 i did complete steps for APNS , 我为APNS做了完整的步骤,

Here's whatever i tried is, where i am wrong? 这是我试过的,我错了?

PHP Code : 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 CODE 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];

  }
}

I got the solution and get the push notification. 我得到了解决方案并获得推送通知。

The problem was with creation of p12 key to pem file. 问题是为pem文件创建了p12键。 I was using this command in terminal for converting file to pem from p12 : 我在终端中使用此命令将文件从p12转换为pem:

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

which removes the certificates "-nocerts" so i used following command to do same operation : 删除证书“-nocerts”所以我使用以下命令来执行相同的操作:

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

and then do as per this link and it will work perfectly. 然后根据此链接执行它将完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM