簡體   English   中英

當我的應用程序被終止時不會收到通知

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM