当我的应用程序处于后台或前台时,我可以收到通知,但是当我的应用程序被杀死时。我无法收到通知。 我使用fcm ... 这是我通过邮递员打电话的有效载荷。
{
"registration_ids":
["...."],
"notification":{
"sound":"default",
"title":"testNotif",
"body":"welcome in the shop Apple owner"
}
}
这是我的 appdelegate :
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_,_ in })
UNUserNotificationCenter.current().delegate = self
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FIRApp.configure()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.prod)
}
func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connect { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
if let refreshedToken = FIRInstanceID.instanceID().token() {
Common.tokenFCM = refreshedToken
print("InstanceID token: \(refreshedToken)")
}
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
connectToFcm()
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
private func userNotificationCenter(center: UNUserNotificationCenter,
willPresentNotification notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print("<<<<<<< %@", userInfo)
}
}
extension AppDelegate : FIRMessagingDelegate {
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print(">>>>> %@", remoteMessage.appData)
}
}
答案 0 :(得分:3)
嗨使用这种json格式
{
"registration_ids":
["...."],
"priority":"high",
"notification":{
"sound":"default",
"title":"testNotif",
"body":"welcome in the shop Apple owner"
}
}
答案 1 :(得分:1)
在这种情况下存在优先级问题。如果您没有将其设置为正常优先级,则可能无法立即显示通知。如果您将high
设置为优先级,则意味着您会立即收到通知。可能是因为iOS捆绑通知处理它的方式。
高优先级做什么:(即时消息)
尝试立即发送消息,只要设备出现通知,就会立即与您的应用服务器建立网络连接。但是你需要考虑的另一件事是设备的功耗。高优先级比正常电池消耗更多电池。
有关详情和详情,请查看Firebase: Setting the priority of a message 和Apple Doc