gpt4 book ai didi

ios - 应用程序在后台时不显示推送通知

转载 作者:行者123 更新时间:2023-11-28 09:29:34 25 4
gpt4 key购买 nike

我正在尝试在我的 IOS 应用程序中实现 Firebase 推送通知,但无法弄清楚当应用程序处于后台时如何接收通知。

我使用 print 来显示通知,但它只在应用程序打开时打印通知。如果我在应用程序处于后台时发送通知,什么也不会发生,但消息会在我重新打开应用程序时立即打印出来。

下面是我的 AppDelegate 代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

registerForPushNotifications(application)
FIRApp.configure()

// Add observer for InstanceID token refresh callback.
NSNotificationCenter
.defaultCenter()
.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)

// Override point for customization after application launch.
return true
}

func registerForPushNotifications(application: UIApplication) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
sendNotification();

print("===== didReceiveRemoteNotification ===== %@", userInfo)
}




func tokenRefreshNotificaiton(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")
connectToFcm()
}

func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
func sendNotification() {
let notification = UILocalNotification()
let dict:NSDictionary = ["ID" : "your ID goes here"]
notification.userInfo = dict as! [String : String]
notification.alertBody = "title"
notification.alertAction = "Open"
notification.fireDate = NSDate()
UIApplication.sharedApplication().scheduleLocalNotification(notification)


}


}

我也已经添加到我的 info.plist

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>

这是我发送的消息的格式

{
"to" : "",
"notification" : {
"body" : "",
"title" : "",
},
"content_available": true,
"priority": "high"
}

最佳答案

当您的应用程序关闭时,您无法接收推送通知数据。您应该将消息存储到服务器,而不是用户从那里访问消息

关于ios - 应用程序在后台时不显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063355/

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