gpt4 book ai didi

ios - 推送通知不会快速调用 didReceiveRemoteNotification

转载 作者:行者123 更新时间:2023-11-30 12:56:27 73 4
gpt4 key购买 nike

我已经使用 FCM 实现了推送通知。当我的应用程序位于前台并且通知到达时,将调用 didReceiveRemoteNotification 方法,但当应用程序未处于事件状态时,它不会调用此方法

当应用程序在后台时,我通过将 bool 值设置为 true 来检查这一点,但不更改 bool 值,这意味着它不会执行此行。这是我的方法。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in
if UIApplication.sharedApplication().applicationState != .Active{


NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification")
NSUserDefaults.standardUserDefaults().synchronize()
}else{

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if UIApplication.sharedApplication().applicationState != .Active{


NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification")
NSUserDefaults.standardUserDefaults().synchronize()
}else{

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
completionHandler(.NewData)
}

为了注册 GCM 推送通知,我在用户成功登录时使用这些方法。

func userDidLoggedIn() -> Void {
tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! VaboTabBarController


self.window?.rootViewController = self.tabBarController
self.window?.makeKeyAndVisible()

self.registerDeviceForPushNotification(UIApplication.sharedApplication())

}
func connectToFcm() {

FIRMessaging.messaging().connectWithCompletion({error in


if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}


})
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}


func registerDeviceForPushNotification(application:UIApplication) -> Void {

let settings: UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: [.Alert,.Badge,.Sound], categories: nil)
self.pushNotificationToken = FIRInstanceID.instanceID().token()!
let userID = self.userData["id"] as! NSNumber

print("InstanceID token: \(self.pushNotificationToken)")
self.registerDeviceOnServerWith(self.pushNotificationToken, userID: userID)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}


func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {

let userID = self.userData["id"] as! NSNumber

self.registerDeviceOnServerWith(refreshedToken, userID: userID)
print("InstanceID token: \(refreshedToken)")
}

// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
PS。我已经从功能以及 info.plist 中设置了远程通知检查。另外,我还尝试从通知负载中添加 "content-available":1

最佳答案

您必须在通知负载中写入正确的拼写和字符位置。根据您的陈述,您可能在内容可用的位置放置了不正确的字符。请以此格式发送您的有效负载..

{
"aps":{
"alert":{
"title":"Notification Title Text",
"Body" :"Notification Body",
"content_available":1

}
}
}

您已经编写了content-available,它应该是content_availble。现在这将唤醒您的应用程序以在后台运行您的逻辑

关于ios - 推送通知不会快速调用 didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40341439/

73 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com