gpt4 book ai didi

PHP-iOS 带图像的推送通知

转载 作者:行者123 更新时间:2023-11-29 05:55:51 25 4
gpt4 key购买 nike

我正在尝试将带有图像内容的推送通知从我的 Web 应用程序发送到 iOS 应用程序。我收到了包含我所提供的所有文字和正文的通知。但给定的图像未显示在通知中。

$url = 'https://fcm.googleapis.com/fcm/send';
$token = "*******************";
$title = "Title";
$body = "This is Test Notification";
$notification = array('title' =>$title , 'text' => $body, 'subtitle'=>'Sub title', 'sound' => 'default', 'badge' => '1', 'category' => 'CustomSamplePush', 'mutable-content'=>'1','urlImageString'=>'imageurl');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');

$fields = json_encode($arrayToSend);
echo $fields;
$headers = array (
'Authorization: key=' . "***********",
'Content-Type: application/json',
'authKey: keyhere',
'authKeyId:****',
'teamId: ****',
'bundleId: *****',
'endpoint: https://api.development.push.apple.com'
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );

最佳答案

要显示图像、音频和视频等媒体内容,您需要在 iOS 应用程序中添加NotificationServiceExtension。要在 iOS 应用程序中执行NotificationServiceExtension,您需要将可变内容值发送为 1,这在您提到的有效负载中看起来很好。在NotificationServiceExtension中,您将有大约10秒的时间从您在通知负载中发送的URL下载图像。下载图像后,您需要将图像保存在文件管理器中。之后,您使用文件图像 URL 初始化 UNNotificationAttachment 并将其传递给完成处理程序。 PFA 代码如下

import UserNotifications

类NotificationService:UNNotificationServiceExtension {

var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
var urlString:String? = nil
if let urlImageString = request.content.userInfo["urlImageString"] as? String {
urlString = urlImageString
}

if urlString != nil, let fileUrl = URL(string: urlString!) {
print("fileUrl: \(fileUrl)")

guard let imageData = NSData(contentsOf: fileUrl) else {
contentHandler(bestAttemptContent)
return
}
guard let attachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "image.jpg", data: imageData, options: nil) else {
print("error in UNNotificationAttachment.saveImageToDisk()")
contentHandler(bestAttemptContent)
return
}

bestAttemptContent.attachments = [ attachment ]
}

contentHandler(bestAttemptContent)
}
}

override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}

}

@available(iOSApplicationExtension 10.0, *)扩展 UNNotificationAttachment {

static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)

do {
try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL!, options: [])
let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
return attachment
} catch let error {
print("error \(error)")
}

return nil
}

}

关于PHP-iOS 带图像的推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55175920/

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