W
W
wolfak2019-11-14 09:32:49
Swift
wolfak, 2019-11-14 09:32:49

Why does IOS receive one push but two notifications are displayed?

Good afternoon.
I send a push notification from the backend part of the app to the iOS and Android app
.

Array ( [registration_ids] => Array ( [0] => test_token ) [notification] => Array ( [title] => Notification title [body] => Notification text [sound] => 1 [content_available] => 1 [ priority] => high [badge] => 4 [smallIcon] => small_logo [click_action] => .NoticeFullController ) [data] => Array ( [short_text] => Notification short text [selectNotice] => 123 [selectStatus] = > 4 ) [apns] => Array ( [headers] => Array ( [apns-priority] => 5 ) [payload] => Array ( [aps] => Array ( [category] => notice_push ) ) ) )

There are no problems with the Android application, but the following happens in iOS:
1. If the application is in the background or the notification is closed, one notification is displayed, but when you click on it, the main page of the application opens, but you need to open the page with the full notification view with the ID from the 'selectNotice' field in the request if it is greater than 0, otherwise the main page.
2. If the application is open, then at the first start there is only a beep of the application and nothing else. A notification should be displayed, and not just its sound.
3. The next time a push is sent, two notifications are displayed, one of which is from the background mode, the second is created by my code, and I need to have only one. And when you click on it, the main page of the application opens, but you need to open a page with a full view of the notification with the ID from the 'selectNotice' field in the request if it is greater than 0, otherwise the main page.
Help with push notifications in iOS. Thank you very much in advance. My AppDelegate Code:
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)
            }
        }

    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question