当我在 iOS 应用程序上收到静默推送通知时,如何启动应用程序或来电 UI?
How do I launch an App or the Incoming Call UI when I receive a silent push notification on an iOS app?
我正在尝试让应用程序在收到静默通知时启动应用程序或来电UI。
目前我有通知工作,我可以发送通知并在收到通知时打印日志
此函数处理通知的接收:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
print("Enitre message \(userInfo)")
let state: UIApplication.State = application.applicationState
switch state {
case UIApplication.State.active:
print("State: App is active")
case UIApplication.State.inactive:
print("State: App is inactive")
case UIApplication.State.background:
print("State: App is running in the background")
default:
print("State: Unknown")
}
completionHandler(UIBackgroundFetchResult.newData)
}
这个功能是否可以打开应用或来电UI?
首先在
中注册 VOIP 通知
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
requestPushKit()
return true
}
导入通话套件
import CallKit
注册推送包
fileprivate func requestPushKit() {
let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
voipRegistry.delegate = self
voipRegistry.desiredPushTypes = [.voIP]
}
VOIP(Push Kit)代表
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry( registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
if type == PKPushType.voIP {
let tokenParts = pushCredentials.token.map { data -> String in
return String(format: "%02.2hhx", data)
}
let tokenString = tokenParts.joined()
print(tokenString)
}
}
func pushRegistry( registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
}
func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type:PKPushType, completion: @escaping () -> Void) {
if type == PKPushType.voIP {
self.incomingCall()
}
}
func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
if type == PKPushType.voIP {
self.incomingCall()
}
}
}
调用incomingcall方法
fileprivate func defaultConfig() -> CXProviderConfiguration{
let config = CXProviderConfiguration(localizedName: "My App")
config.includesCallsInRecents = true
config.supportsVideo = true
config.maximumCallGroups = 5
config.maximumCallsPerCallGroup = 10
// config.iconTemplateImageData = UIImagePNGRepresentation(UIImage(named: "pizza")!)
// config.ringtoneSound = "ringtone.caf"
return config
}
func incomingCall(){
let provider = CXProvider(configuration: defaultConfig())
provider.setDelegate(self, queue: nil)
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .generic, value: "Pete Za")
update.hasVideo = true
provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
}
并调用 Kit 委托方法
extension AppDelegate : CXProviderDelegate {
func providerDidReset( provider: CXProvider) {
}
func providerDidBegin( provider: CXProvider) {
}
func provider( provider: CXProvider, perform action: CXAnswerCallAction) {
action.fulfill()
}
func provider( provider: CXProvider, perform action: CXEndCallAction) {
action.fulfill()
}
func provider( provider: CXProvider, perform action: CXStartCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetHeldCallAction) {
}
func provider( provider: CXProvider, timedOutPerforming action: CXAction) {
}
func provider( provider: CXProvider, perform action: CXPlayDTMFCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetGroupCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetMutedCallAction) {
}
// func provider(_ provider: CXProvider, execute transaction: CXTransaction) -> Bool {
//
// }
}
不过,如果您有任何需要,请告诉我们。 :)
我正在尝试让应用程序在收到静默通知时启动应用程序或来电UI。
目前我有通知工作,我可以发送通知并在收到通知时打印日志
此函数处理通知的接收:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
print("Enitre message \(userInfo)")
let state: UIApplication.State = application.applicationState
switch state {
case UIApplication.State.active:
print("State: App is active")
case UIApplication.State.inactive:
print("State: App is inactive")
case UIApplication.State.background:
print("State: App is running in the background")
default:
print("State: Unknown")
}
completionHandler(UIBackgroundFetchResult.newData)
}
这个功能是否可以打开应用或来电UI?
首先在
中注册 VOIP 通知func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
requestPushKit()
return true
}
导入通话套件
import CallKit
注册推送包
fileprivate func requestPushKit() {
let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
voipRegistry.delegate = self
voipRegistry.desiredPushTypes = [.voIP]
}
VOIP(Push Kit)代表
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry( registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
if type == PKPushType.voIP {
let tokenParts = pushCredentials.token.map { data -> String in
return String(format: "%02.2hhx", data)
}
let tokenString = tokenParts.joined()
print(tokenString)
}
}
func pushRegistry( registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
}
func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type:PKPushType, completion: @escaping () -> Void) {
if type == PKPushType.voIP {
self.incomingCall()
}
}
func pushRegistry( registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
if type == PKPushType.voIP {
self.incomingCall()
}
}
}
调用incomingcall方法
fileprivate func defaultConfig() -> CXProviderConfiguration{
let config = CXProviderConfiguration(localizedName: "My App")
config.includesCallsInRecents = true
config.supportsVideo = true
config.maximumCallGroups = 5
config.maximumCallsPerCallGroup = 10
// config.iconTemplateImageData = UIImagePNGRepresentation(UIImage(named: "pizza")!)
// config.ringtoneSound = "ringtone.caf"
return config
}
func incomingCall(){
let provider = CXProvider(configuration: defaultConfig())
provider.setDelegate(self, queue: nil)
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .generic, value: "Pete Za")
update.hasVideo = true
provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
}
并调用 Kit 委托方法
extension AppDelegate : CXProviderDelegate {
func providerDidReset( provider: CXProvider) {
}
func providerDidBegin( provider: CXProvider) {
}
func provider( provider: CXProvider, perform action: CXAnswerCallAction) {
action.fulfill()
}
func provider( provider: CXProvider, perform action: CXEndCallAction) {
action.fulfill()
}
func provider( provider: CXProvider, perform action: CXStartCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetHeldCallAction) {
}
func provider( provider: CXProvider, timedOutPerforming action: CXAction) {
}
func provider( provider: CXProvider, perform action: CXPlayDTMFCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetGroupCallAction) {
}
func provider( provider: CXProvider, perform action: CXSetMutedCallAction) {
}
// func provider(_ provider: CXProvider, execute transaction: CXTransaction) -> Bool {
//
// }
}
不过,如果您有任何需要,请告诉我们。 :)