1

我已经成功地为我的 ios 应用程序实现了推送通知服务。我已经生成了所需的证书并且代码有效。当设备与互联网断开连接并接收到一些通知(待处理且未显示)时,就会出现问题,然后当设备再次连接到互联网时......仅显示一个待处理的apns推送通知。我将php用于我的后端,将 NotificationServiceExtension 用于附件等。

这是我的php代码

public static function sendAPNS($token,$data)
    {
      print_r($token);
      $apnsServer = 'ssl://gateway.push.apple.com:2195';
      $privateKeyPassword = 'password-here';
      /* Device token */
      $deviceToken = $token;
      $pushCertAndKeyPemFile = 'nameofthefile.pem';
      $stream = stream_context_create();
      stream_context_set_option($stream, 'ssl', 'passphrase', $privateKeyPassword);
      stream_context_set_option($stream, 'ssl', 'local_cert', $pushCertAndKeyPemFile);
      $connectionTimeout = 20;
      $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
      $connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout, $connectionType, $stream);




        /*Alert array eg. body, title etc */
      $alertArray = [];
      $alertArray["body"] = $data["body"];
      $alertArray["title"] =$data["title"];


      $messageBody['aps'] = array(
        'alert' => $alertArray,
        'sound' => 'default',
        'category'=> 'customUi',
        'mutable-content'=>1

      );
      /*User Info*/
      $messageBody["attachment-url"] = $data["url"];
      $messageBody["type_code"] = $data["type_code"];
      $messageBody["ref_id"] = $data["ref_id"];
      $messageBody["user_to"] =$data["user_to"];

      /*Could be here*/



      $payload = json_encode($messageBody);

       print_r($payload);
      $notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
      $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));


      fclose($connection);
    }

我的服务扩展如下:-

class 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 {


        var defaultsUser: UserDefaults = UserDefaults(suiteName: "group.shared.com.plates215")!
        if let countNot = defaultsUser.value(forKey: "notUniversal")
        {
            var   IntcountNot = countNot as! Int
            IntcountNot = IntcountNot + 1
            var sum: NSNumber = NSNumber(value: IntcountNot)
            bestAttemptContent.badge = sum
            defaultsUser.set(IntcountNot, forKey: "notUniversal") 

        }

        if let photo = bestAttemptContent.userInfo["attachment-url"] as? String
        {
            updateReadNots(userID: (bestAttemptContent.userInfo["user_to"] as? String)!)
            let url = NSURL(string: photo);
            var err: NSError?
            var imageData :NSData = try! NSData(contentsOf: url! as URL)
            var bgImage = UIImage(data:imageData as Data)

            if let attachment = UNNotificationAttachment.create(identifier: "colo", image: bgImage!, options: nil)
            {
                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)
    }
}
func updateReadNots(userID: String)
{
    var url: String = "api-url"
    let session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
    var urlDown = URL(string: url)
    let downloadTask = session.downloadTask(with: urlDown!) { (url, rsp, error) in


    }
    downloadTask.resume()


}

     }

    extension UNNotificationAttachment {

static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) ->  UNNotificationAttachment? {
    let fileManager = FileManager.default
    let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
    let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
    do {
        try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil)
        let imageFileIdentifier = identifier+".png"
        let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier)
        guard let imageData = UIImagePNGRepresentation(image) else {
            return nil
        }
        try imageData.write(to: fileURL)
        let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options)
        return imageAttachment
    } catch {
        print("error " + error.localizedDescription)
    }
    return nil
}
  }

解决方案
根据@Li Sim 的链接,这就是我所做的......我从我的服务器发送的每个通知不仅包含最新通知,还包含由“\n”分隔的每个未读通知。然后 ios 通知会正确显示它们。为了跟踪已读和未读通知,我在后端保留了一个 read_status 键,一旦在 NotificationContentExtension中收到通知,该键就会更新 (https://developer.apple.com/documentation/usernotifications/unnotificatio nserviceextension )。它用于在收到 apns 推送时修改内容并执行一些代码。希望这对将来的某人有所帮助:)

4

1 回答 1

0

这是 Apple 之前记录的行为。文档链接:https ://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

如果设备离线,发送针对该设备的通知请求会导致先前的请求被丢弃。如果设备长时间处于离线状态,则其在 APN 中存储的所有通知都将被丢弃。

虽然我对此不熟悉,但(对此感到抱歉)这里有一些我发现对您的问题有用的帖子: 当 APNS 仅存储一个以防设备离线时,whatsapp 如何接收多个通知?

希望你能从中有所收获。我能想到的另一种方法是使用与离线兼容的 LocalNotifications,但您必须找出一种方法来跟踪用户未阅读的通知。(也许是某种状态)

于 2018-11-02T05:56:35.547 回答