带图像的 PHP-iOS 推送通知

Posted

技术标签:

【中文标题】带图像的 PHP-iOS 推送通知【英文标题】:PHP-iOS push-notifications with image 【发布时间】:2019-03-15 05:11:21 【问题描述】:

我正在尝试将带有图像内容的推送通知从我的 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 ); 

【问题讨论】:

也许这会对你有所帮助。 ***.com/questions/37839171/… 【参考方案1】:

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

import UserNotifications

类通知服务: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 推送通知的主要内容,如果未能解决你的问题,请参考以下文章

带计时器的推送通知 - iPhone

带参数的推送通知不接收 Windows Phone

iOS - 仅带声音的推送通知

如何在推送通知 (GCM) android 中发送图像?

通过推送通知服务添加图像?

用图像反应原生 iOS 推送通知