出于某种原因,我的didReceiveRemoteNotification永远不会被调用。我做了以下事情:
APNS清单:
使用推送通知
创建允许的AppId使用有效证书和应用ID
创建SSL证书使用相同的证书创建配置文件,并确保添加设备
使用代码:
注册推送通知的应用
处理didRegisterForRemoteNotificationsWithDeviceToken方法
设定目标>能力>背景模式>远程通知 处理didReceiveRemoteNotification
但我的功能似乎没有被调用。我的代码如下所示:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if (application.respondsToSelector("isRegisteredForRemoteNotifications"))
{
// iOS 8 Notifications
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
application.registerForRemoteNotifications()
}
else
{
// iOS < 8 Notifications
application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for var i = 0; i < deviceToken.length; i++ {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
apnsID = tokenString
println("******apnsID is \(apnsID)")
dToken = deviceToken
println("******dToken is \(dToken)")
NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "deviceToken")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("***********didFailToRegisterForRemoteNotificationsWithError")
println(error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
println("Getting notification")
var message: NSString = ""
var alert: AnyObject? = userInfo["aps"]
println(userInfo["aps"])
if((alert) != nil){
var alert = UIAlertView()
alert.title = "Title"
alert.message = "Message"
alert.addButtonWithTitle("OK")
alert.show()
}
}
答案 0 :(得分:0)
我发现自己也遇到了同样的问题,为了调用didReceiveRemoteNotification
,传入的通知音乐带有一个"available_content" : true
参数和通知有效载荷。因此,如果您以字典形式发送通知,就像我在设备到设备推送中的情况一样,只需将其添加到字典中,就像您为sound
或{{ 1}},如果您将某种推送服务用作Postman,则也需要
答案 1 :(得分:0)
三个步骤:
在此处添加处理传入通知的逻辑:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // 在此处添加您的逻辑
completionHandler(.newData)
}
为目标中的功能添加后台模式,并确保选中“远程通知”
在发送推送通知时,添加 "content_available" : true
这在 iOS 14/Xcode 12.3 中对我有用。