Swift 两次接收推送通知有效负载数据

Swift 两次接收推送通知有效负载数据,swift,push-notification,swift5,payload,xcode11.5,Swift,Push Notification,Swift5,Payload,Xcode11.5,每当我从firebase控制台发送推送通知时,它会出现一次,但当我以编程方式发送时,它会被设备接收两次。我正在使用实时firebase日期和云函数。 这是我的AppDelegate.swift @UIApplicationMain 类AppDelegate:UIResponder、UIApplicationLegate{ 变量窗口:UIWindow?/------------------------------------------------------------------------

每当我从firebase控制台发送推送通知时,它会出现一次,但当我以编程方式发送时,它会被设备接收两次。我正在使用实时firebase日期和云函数。 这是我的AppDelegate.swift

@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate{ 变量窗口:UIWindow?/------------------------------------------------------------------------------------------------------------------------------------------------------------------//

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "mChat")
    container.loadPersistentStores(completionHandler: {
        (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

// ---------------------------------------------------------------------------------------------------------------------------------------------------- //

func saveContext() {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do{
            try context.save()
        }catch{
            let error = error as NSError
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    }
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------- //

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

 FirebaseApp.configure()
 
 
 self.registerForPushNotifications(application)
         return true
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------- //

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------- //

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    guard let userId = CurrentUser.uid else { return }
    let userRef = Database.database().reference().child("userActions").child(userId)
    userRef.removeValue()
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------- //

 
 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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
   }

   func applicationDidEnterBackground(_ application: UIApplication) {
       // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
       // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   }

   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.
   }

   func applicationDidBecomeActive(_ application: UIApplication) {
       // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
   }

   func applicationWillTerminate(_ application: UIApplication) {
       // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
   }
这是我的云函数

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.newMessage = functions.database.ref('messages/{uid}/{friendId}/{messageId}').onWrite((event, context) => {

    const discussionId = context.params.uid;
    const messageId = context.params.messageId;

    const message = event.after.val();

   

    var message_value = message.sender;
var messagebody = message.message;
    const setNewMessagePromise = admin.database().ref(`/users/${message_value}/name`).once('value').then(snapshot => {
const messageContent = snapshot.val();   
 const payload = {
        notification: {
            title: messageContent,
            body: messagebody,
 sound: 'default'
        }
    } 
 const options = {
            priority: 'high',
            timeToLive: 60 * 60 * 24
        };   
const sendMessagePromise = admin.messaging().sendToTopic("ALL", payload, options);
  return Promise.all([setNewMessagePromise, sendMessagePromise]);   
 }); 

});
需要帮助为什么我收到两次通知

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.newMessage = functions.database.ref('messages/{uid}/{friendId}/{messageId}').onWrite((event, context) => {

    const discussionId = context.params.uid;
    const messageId = context.params.messageId;

    const message = event.after.val();

   

    var message_value = message.sender;
var messagebody = message.message;
    const setNewMessagePromise = admin.database().ref(`/users/${message_value}/name`).once('value').then(snapshot => {
const messageContent = snapshot.val();   
 const payload = {
        notification: {
            title: messageContent,
            body: messagebody,
 sound: 'default'
        }
    } 
 const options = {
            priority: 'high',
            timeToLive: 60 * 60 * 24
        };   
const sendMessagePromise = admin.messaging().sendToTopic("ALL", payload, options);
  return Promise.all([setNewMessagePromise, sendMessagePromise]);   
 }); 

});