0

didReceiveRemoteNotification使用静默通知功能以编程方式绘制通知。除非用户重新启动手机并且至少没有应用一次,否则一切正常。

代码 :

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
    
    let localNotification = UNMutableNotificationContent()
    localNotification.sound = UNNotificationSound.default
    localNotification.userInfo = userInfo
    
    
    if (UIApplication.shared.applicationState != .inactive) {
        
        if UserDetails.language == "ar"{
            localNotification.body = (userInfo["display_message_ar"] as? String ?? "").withoutHtml
            localNotification.title = (userInfo["title"] as? String ?? "").withoutHtml
        }else{
            localNotification.body = (userInfo["display_message_en"] as? String ?? "").withoutHtml
            localNotification.title = (userInfo["title"] as? String ?? "").withoutHtml
        }
        
    }
    
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
    let request = UNNotificationRequest(identifier: "TempPush", content: localNotification, trigger: trigger)
    let center = UNUserNotificationCenter.current()
    
    if (UIApplication.topViewController() as? ChatVC) != nil {
        
    }
    else
    {
        center.add(request, withCompletionHandler: { error in
        })
    }
    
    completionHandler(UIBackgroundFetchResult.newData)
}

背景模式:

在此处输入图像描述

4

0 回答 0