【发布时间】:2016-10-20 08:42:19
【问题描述】:
我在 iOS 上遇到了 FCM 通知问题。
当我的应用程序在前台时,我收到成功通知(appdelegate 中的回调 didReceiveRemoteNotification 被触发),但是当应用程序在后台时我没有收到通知(我在通知中看不到任何内容iOS 托盘)。
所以,我认为问题在于 FCM 发送的消息格式。 我的服务器发给FCM的json格式如下:
{
"data":{
"title":"mytitle",
"body":"mybody",
"url":"myurl"
},
"notification":{
"title":"mytitle",
"body":"mybody"
},
"to":"/topics/topic"
}
如您所见,我的 json 中有两个块:一个通知块(在后台接收通知),一个数据块(在前台接收通知)。
我不明白为什么没有收到后台通知。 我怀疑块的顺序(如果我把“数据”块放在“通知”块之前有问题吗?)。
编辑: 有关该问题的更多信息。
这是我的 appdelegate.swift:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
// Application started
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
{
let pushNotificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tokenRefreshNotification:", name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
// Handle refresh notification token
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
// Connect to FCM since connection may have failed when attempted before having a token.
if (refreshedToken != nil)
{
connectToFcm()
FIRMessaging.messaging().subscribeToTopic("/topics/topic")
}
}
// Connect to FCM
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
// Handle notification when the application is in foreground
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"])")
// Print full message.
print("%@", userInfo)
}
// Application will enter in background
func applicationWillResignActive(application: UIApplication)
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
// Application entered in background
func applicationDidEnterBackground(application: UIApplication)
{
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
// Application will enter in foreground
func applicationWillEnterForeground(application: UIApplication)
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
// Application entered in foreground
func applicationDidBecomeActive(application: UIApplication)
{
connectToFcm()
application.applicationIconBadgeNumber = 0;
}
// Application will terminate
func applicationWillTerminate(application: UIApplication)
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
我可以在前台接收消息的唯一方法是禁用方法调配,在我的 info.plist 中将 FirebaseAppDelegateProxyEnabled 设置为 NO。
在这种情况下,FCM 文档说我必须在我的 appdelegate.swift 中实现两种方法:
- FIRMessaging.messaging().appDidReceiveMessage(userInfo) in didReceiveRemoteNotification callback
- FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) in didRegisterForRemoteNotificationsWithDeviceToken callback
但是如果我实现了这些功能,即使应用程序在前台,消息也会停止到达。
我知道这很奇怪。
编辑 2:
当应用程序在后台时,没有收到通知,但是当我打开我的应用程序时,会立即收到相同的通知(触发方法 didReceiveRemoteNotification)。
【问题讨论】:
-
只是出于兴趣,您是否尝试将优先级设置为高?
-
是的,但没有任何改变。后台仍然没有通知。
-
尝试将content_available 添加为真值。如果它不起作用,请尝试在使用
content_available时删除priority。我对 APNS 的工作原理不是很熟悉,但是如果priority和content_available都在一个有效负载中,则该消息可能会被忽略。让我知道它是否有效。干杯! -
你试过了吗@MarkO'Brian?成功了吗?
-
我遇到了同样的问题,但我已经将优先级设置为
High,但这并没有帮助
标签: ios swift firebase google-cloud-messaging firebase-cloud-messaging