Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么只有一个推送式iOS而只有两个通知?_Ios_Swift_Xcode_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

为什么只有一个推送式iOS而只有两个通知?

为什么只有一个推送式iOS而只有两个通知?,ios,swift,xcode,push-notification,firebase-cloud-messaging,Ios,Swift,Xcode,Push Notification,Firebase Cloud Messaging,我从iOS和Android应用程序的后端发送推送通知 我在推送请求中查看了以下内容: 数组([registration\u ids]=>Array([0]=>test\u token)[notification]=>Array([title]=>title of notification[body]=>Text of notification[sound]=>1[content\u available]=>1[priority]=>high[badge]=>4[smallIcon]=>small

我从iOS和Android应用程序的后端发送推送通知 我在推送请求中查看了以下内容:

数组([registration\u ids]=>Array([0]=>test\u token)[notification]=>Array([title]=>title of notification[body]=>Text of notification[sound]=>1[content\u available]=>1[priority]=>high[badge]=>4[smallIcon]=>small\u logo[点击动作]=>。NoticelController)[数据]=>Array([短文本]=>通知短文本[selectNotice]=>123[selectStatus]=>4[APN]=>Array([headers]=>Array([apns priority]=>5)[payload]=>Array([aps]=>Array([category]=>notice\u push)))

Android应用程序没有问题,但在iOS上会发生以下情况:

  • 如果应用程序位于后台或通知已关闭,则会显示一个,但当您单击该应用程序时,会打开该应用程序的主页面,如果该主页面大于0,则需要打开一个带有通知完整视图的页面,该页面的ID来自请求中的“SelectNotification”字段,否则将显示主页面

  • 如果应用程序是打开的,那么在第一次启动时,只有应用程序的声音,没有其他声音。而且应该显示一个通知,而不仅仅是它的声音

  • 下次发送推送时,将显示两个通知,其中一个来自后台模式,第二个由我的代码创建,但您只需要一个。当您单击它时,应用程序的主页面将打开,但如果该页面大于0,则需要打开该页面,并显示通知的完整视图,ID来自请求中的“SelectNotification”字段,否则将显示主页面

  • 帮助处理推送iOS。事先非常感谢。我的AppDelegate代码:

    import UserNotifications
    import Firebase
    import FirebaseMessaging
    
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        // Use Firebase library to configure APIs
        FirebaseApp.configure()
    
        // Уведомления в фоне
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})
    
        // Override point for customization after application launch.
        return true
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler([.alert, .badge, .sound])
    }
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        //Handle the notification
        completionHandler(
            [UNNotificationPresentationOptions.alert,
             UNNotificationPresentationOptions.sound,
             UNNotificationPresentationOptions.badge])
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        Messaging.messaging().subscribe(toTopic: "notice_push")
    }
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    
        let content = UNMutableNotificationContent()
    
        if let title = userInfo["title"]
        {
            content.title = title as! String
        }
    
        if let short_text = userInfo["short_text"]
        {
            content.body = short_text as! String
        } else if let short_text = userInfo["body"]
        {
            content.body = short_text as! String
        }
    
        if let badge = userInfo["badge"]
        {
            UIApplication.shared.applicationIconBadgeNumber = badge as! Int
        }
        //category = ".NoticeFullController";
    
        content.userInfo = userInfo
        content.sound = .default()
        content.threadIdentifier = "my-notice"
        if #available(iOS 12.0, *) {
            content.summaryArgument = "notification"
        } else {
            // Fallback on earlier versions
        }
    
        UNUserNotificationCenter.current().delegate = self
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.5, repeats: false)
        let request = UNNotificationRequest(identifier:"notice", content: content, trigger: trigger)
    
        UNUserNotificationCenter.current().add(request) { (error) in
            if let getError = error {
                print(getError.localizedDescription)
            }
        }
    
    }
    }
    

    您有两个通知,因为当您的应用程序位于前台时调用
    func应用程序(u应用程序:UIApplication,didReceiveMemotentification用户信息:[AnyHashable:Any])
    ,当您的应用程序位于后台时单击通知时调用

    此方法已弃用

    您只需实现这两个委托方法

    func userNotificationCenter(center:unuservificationcenter,willPresent通知:UNNotification,with completionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void)
    在应用程序位于前台时调用


    func-userNotificationCenter(center:unuservificationcenter,didReceive-response:UNNotificationResponse,withCompletionHandler completionHandler:@escaping()->Void)
    在应用程序处于后台时单击通知时被调用。

    谢谢。当我单击通知并将数据从“selectNotice”传递给所选页面时,如何启动该页面?谢谢