【问题标题】:dont get notification when my app is kill当我的应用程序被杀死时没有收到通知
【发布时间】:2016-10-18 11:10:01
【问题描述】:

当我的应用程序处于后台或前台时,我可以收到通知,但是当我的应用程序被 kill 时。我无法收到通知。 我用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)

    }
}

【问题讨论】:

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

你好用这个json格式

{

  "registration_ids":
    ["...."],
  "priority":"high",

  "notification":{  
     "sound":"default",
     "title":"testNotif",
     "body":"welcome in the shop Apple owner"
  }

}

【讨论】:

    【解决方案2】:

    在这种情况下,优先级存在问题。如果您没有设置它意味着在正常优先级,它可能有机会不会立即出现通知。如果您将优先级设置为high,则意味着您会立即收到通知。可能是因为 iOS 捆绑通知的方式是这样处理的。

    那么高优先级有什么作用:(即时消息)

    尝试立即传递消息,并在您的设备出现通知后立即与您的应用服务器建立网络连接。但是您需要考虑的另一件事是设备的功耗。高优先级比普通优先级消耗更多电池。

    更多信息和详情,您可以查看Firebase: Setting the priority of a message Apple Doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多