[英]Push notification not displaying when app is in background
我正在嘗試將Firebase推送通知實施到我的IOS應用程序中,但無法弄清楚當應用程序在后台時如何接收通知。
我使用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"
}
當您的應用關閉時,您將不會收到推送通知數據。 您應該將消息存儲到服務器,而不是用戶從那里到達消息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.