gpt4 book ai didi

php - 如何运行 php 脚本来发送推送通知

转载 作者:行者123 更新时间:2023-11-29 01:26:28 25 4
gpt4 key购买 nike

我需要帮助快速实现推送通知。我遵循了 ray wenderlich 教程 http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1但它没有提到您实际上是如何在 Xcode 中调用或运行 php 推送通知脚本的。这就是我现在尝试调用脚本的方式:

    let request = NSMutableURLRequest(URL: NSURL(string: "http://website.com/pushNotification")!)
request.HTTPMethod = "POST"

let dataDictionary:[String:String] = ["NotificationData":"\(deviceTokenString)<*&*>password<*&*>my first push notification"]

let data:NSData = try! NSJSONSerialization.dataWithJSONObject(dataDictionary, options: [])
request.HTTPBody = data

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

// Create a NSURLSession task with completion handler
let task:NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

// Convert the data into a dictionary
let response:[String:String] = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! [String:String]


// Check if there was an error
if let result = response["result"] {
if result == "success"{

NSLog("Message deleviered successfully")


}
else if result == "error"{
NSLog("Message could not be deleviered")
}

}

})

// Run the task
task.resume()

这是它命中的 php 脚本:

<?php


// Get the data from the request
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$pushData = $data['NotificationData'];

// Format data
$keywords = explode("<*&*>", $pushData);

// Assign data into variables
$deviceToken = $keywords[0];
$passphrase = $keywords[1];
$message = $keywords[2];

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'signingcertificate.p12');
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);
}


// 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 '{"result" : "error"}';
}
else {
echo '{"result" : "success"}';
}
// Close the connection to the server
fclose($fp);

?>

但是 Xcode 给我这个错误:

fatal error :“尝试!”表达式意外引发错误:Error Domain=NSCocoaErrorDomain Code=3840“JSON 文本未以数组或对象开头,并且允许未设置片段的选项。” UserInfo={NSDebugDescription=JSON 文本不是以数组或对象和允许未设置片段的选项开始的。}:文件/Library/Caches/com.apple.xbs/Sources/swiftlang_PONDEROSA/swiftlang_PONDEROSA-700.1.101.6/src/swift/stdlib/public/core/ErrorType.swift,第 50 行

我几乎肯定这个错误与我调用 php 脚本的方式有关,但我不知道应该如何调用这种类型的 php 脚本。请帮忙!我们将不胜感激您的任何建议或见解!

最佳答案

该链接会告诉您如何处理 PHP 脚本。您不应该通过 Xcode 调用它。

如教程所述:

As I’ve mentioned a few times before, you need to set up a server that sends the push notifications to your app. For this first test, you’re not going to set up a server just yet. Instead, I’ll give you a very simple PHP script that sets up a connection to APNS and sends a push notification to a device token that you specify. You can run this straight from your Mac.

...

You should copy the device token from the app into the $deviceToken variable. Be sure to leave out the spaces and brackets; it should just be 64 hexadecimal characters. Put your private key’s passphrase into $passphrase, and the text you wish to send in $message. Copy your ck.pem file into the SimplePush folder. Remember, the ck.pem file contains both your certificate and the private key.

Then open a Terminal and type: .....

PHP 是您的服务器将执行的操作的一个简单示例。您还需要构建服务器,以便在特定事件发生时调用对苹果 APNS 的调用。移动应用程序本身不会调用推送通知。

关于php - 如何运行 php 脚本来发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33942328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com