duanpu4143 2016-10-20 09:23
浏览 53

iOs 10 - SWIFT 3 - 通知推送

I have a little problem with notification push and iOs 10 (with Swift 3)..

I followed this tutorial : https://www.sitepoint.com/developing-push-notifications-for-ios-10/ With Pusher, it's ok, works fine.. I receive the notification on my phone.

But I would like to send notifications from my web server.. I generate my .pem file (http://www.apptuitions.com/generate-pem-file-for-push-notification/)

My php code :

$apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
$privateKeyPassword = 'mykey';
$message = "My message here !";
$deviceToken ='MYTOKENHERE';

$pushCertAndKeyPemFile = 'pushcert.pem';
$stream = stream_context_create();
stream_context_set_option($stream,'ssl','passphrase',$privateKeyPassword);
stream_context_set_option($stream,'ssl','local_cert',$pushCertAndKeyPemFile);
$connectionTimeout = 30;
$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout,$connectionType,$stream);

if (!$connection){
    echo "Failed to connect to the APNS server. Error = $errorString <br/>";
    exit;
}
else{
    echo "Successfully connected to the APNS. Processing...</br>";
}
$messageBody['aps'] = array('alert' => $message,'badge' => 1, 'sound' => 'default');
$payload = json_encode($messageBody);
$notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));

if (!$wroteSuccessfully){
    echo "Could not send the message<br/>";
} else {
    echo "Successfully sent the message<br/>";
}

Result : Successfully connected to the APNS. Processing... Successfully sent the message But I never receive notification on my phone

Why ? How to send a notification push with php ?

Thanks for your help :)

[EDIT] Ok, i found a solution.. Just add at the end:

fclose($connection);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划