菜鸟教程小白 发表于 2022-12-13 11:39:50

ios - 没有收到推送通知 - ios


                                            <p><p>在我的应用程序中我试图接收推送通知,我创建了一个证书并将其转换为 <code>.pem</code> 文件以在终端中测试,并将证书复制到服务器,当我发送设备端未收到来自服务器的通知。
我的代码如下</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.


if ()
{
    // iOS 8 Notifications
    ];

    ;
}
else
{
    // iOS &lt; 8 Notifications
    [application registerForRemoteNotificationTypes:
   (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

NSString *str = ;

NSString *newString = ;
newString = ;
newString = ;


    NSData *postData = [ dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = ];

    NSMutableURLRequest *request = [[ init] autorelease];
    ]];

    ;

    ;

    ;

    NSURLConnection *conn = [initWithRequest:request delegate:self];

}}
</code></pre>

<p>服务器端代码</p>

<pre><code> $message = &#39;Hello&#39;;
$badge = 3;
$sound = &#39;default&#39;;
$development = true;

$payload = array();
$payload[&#39;aps&#39;] = array(&#39;alert&#39; =&gt; $message, &#39;badge&#39; =&gt; intval($badge), &#39;sound&#39; =&gt; $sound);
$payload = json_encode($payload);

$apns_url = NULL;
$apns_cert = NULL;
$apns_port = 2195;

if($development)
{
$apns_url = &#39;gateway.sandbox.push.apple.com&#39;;
$apns_cert = &#39;/path/to/cert/cert-dev.pem&#39;;
}
else
{
$apns_url = &#39;gateway.push.apple.com&#39;;
$apns_cert = &#39;/path/to/cert/cert-prod.pem&#39;;
}

$stream_context = stream_context_create();
stream_context_set_option($stream_context, &#39;ssl&#39;, &#39;local_cert&#39;, $apns_cert);

$apns = stream_socket_client(&#39;ssl://&#39; . $apns_url . &#39;:&#39; .    $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

//You will need to put your device tokens into the $device_tokens array yourself
$device_tokens = array();

foreach($device_tokens as $device_token)
{
$apns_message = chr(0) . chr(0) . chr(32) . pack(&#39;H*&#39;, str_replace(&#39; &#39;, &#39;&#39;, $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apns_message);
}

@socket_close($apns);
@fclose($apns);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>开发者帐户中可能存在一些证书问题,或者可能是您的设备未添加到配置文件中。您可以通过重新创建配置文件来检查它。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 没有收到推送通知 - ios,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33540334/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33540334/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 没有收到推送通知 - ios