[英]dont get notification when my app is 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)
}
}
嗨,使用这种 json 格式
{
"registration_ids":
["...."],
"priority":"high",
"notification":{
"sound":"default",
"title":"testNotif",
"body":"welcome in the shop Apple owner"
}
}
在这种情况下,优先级存在问题。 如果您未将其设置为正常优先级,则可能有可能不会立即出现通知。 如果您将优先级设置为high
,则意味着您会立即收到通知。 可能是因为 iOS 捆绑通知的处理方式。
那么高优先级是做什么的:(即时消息)
尝试立即发送消息,并在通知出现在您的设备上时立即与您的应用服务器建立网络连接。 但您还需要考虑的另一件事是设备的功耗。 高优先级比正常消耗更多的电池。
有关更多信息和详细信息,您可以查看Firebase:设置消息的优先级和Apple Doc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.