推送通知 iPhone(我的设备未收到通知)

发布于 2024-12-08 10:24:15 字数 3550 浏览 8 评论 0原文

我面临的问题是我的 iOS 设备没有收到任何推送通知。

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] 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);
?>

当 PHP 代码运行时,它会显示输出:

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

但在设备端,没有收到任何通知。

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 sharedApplication] 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-12-15 10:24:15

我遇到了很多与公钥和私钥相关的问题。

最后,我撤销了所有证书并点击以下链接,然后我在设备中收到推送通知。

您还可以在下面的链接中找到php的示例代码

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.

多孤肩上扛 2024-12-15 10:24:15

你的证书有问题。尝试创建新的应用程序 ID 并生成development.cerProduction.cer`。之后生成配置文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文