开发者

Push Notification iphone (my device not receive notification)

I am facing the problem that my iOS device does not receive any push notification(s).

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedA开发者_高级运维pplication] 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);
?>

When the PHP code is run it shows the output:

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

But on the device side, no notifications are received.


I faced lot of problems related to public and private key.

At last I had revoked all certificates and followed the below link and then I'm getting the push notification in device.

You can also find the sample code of php in the below link.


The problem with your certificates. Try to create the new app id and generate the development.cerandproduction.cer`. After that generate provisioning profiles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜