Xcode 8.1 Push Notifications in swift 2.3 with firebase integration not getting?

前端 未结 2 611
故里飘歌
故里飘歌 2020-12-01 22:53

I am working with Xcode 8.1 & swift 2.3 here I am using firebase integration for getting push notifications.I don\'t know why i am not getting notifications.. My cod

相关标签:
2条回答
  • 2020-12-01 23:26

    @Uma Madhavi Try adding below method

    SWIFT

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.alert)
    }
    

    Objective-C

     - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    completionHandler(UNNotificationPresentationOptionAlert);
    

    }

    0 讨论(0)
  • 2020-12-01 23:42

    Since you requested me on another thread's comment section, I am posting this working set of code with the same configuration you have mentioned:

    import UserNotifications
    
    @UIApplicationMain
    
    class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate, UNUserNotificationCenterDelegate {
        var window: UIWindow?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
            //############## FIREBASE ##################
    
            registerForPushNotifications(application)
            FIRApp.configure()
            // Add observer for InstanceID token refresh callback.
            NSNotificationCenter
                .defaultCenter()
                .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification),
                             name: kFIRInstanceIDTokenRefreshNotification, object: nil)
    
            //############ FIREBASE END ################  
    
            return true
        }
    
    
        //MARK: - FIREBASE START
    
        //######################################## FIREBASE START ###########################################
    
        func registerForPushNotifications(application: UIApplication) {
    
            if #available(iOS 10.0, *){
                UNUserNotificationCenter.currentNotificationCenter().delegate = self
                UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
                    if (granted)
                    {
                        UIApplication.sharedApplication().registerForRemoteNotifications()
                    }
                    else{
                        //Do stuff if unsuccessful...
                    }
                })
            }
    
            else{ //If user is not on iOS 10 use the old methods we've been using
                let notificationSettings = UIUserNotificationSettings(
                    forTypes: [.Badge, .Sound, .Alert], categories: nil)
                application.registerUserNotificationSettings(notificationSettings)
            }
        }
    
    
    
        func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
            if notificationSettings.types != .None {
                application.registerForRemoteNotifications()
            }
        }
    
        func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
            var tokenString = ""
    
            for i in 0..<deviceToken.length {
                tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
            }
    
            //Tricky line
            FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
            print("Device Token:", tokenString)
            print("Firebase Token:",FIRInstanceID.instanceID().token())
        }
    
        // [START receive_message]
    
    
        @available(iOS 10.0, *)
        func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
            //Handle the notification
            //Use this place to handle
        }
    
    
        @available(iOS 10.0, *)
        func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
            //Handle the notification
            //Use this place to handle the notification
            print(response)
        }
    
        func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                         fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    
            print(userInfo)
        }
    
        // [END receive_message]
    
        // [START refresh_token]
        func tokenRefreshNotification(notification: NSNotification) {
            if let refreshedToken = FIRInstanceID.instanceID().token() {
                print("InstanceID token: \(refreshedToken)")
            }
    
            // Connect to FCM since connection may have failed when attempted before having a token.
            connectToFcm()
        }
    
        // [END refresh_token]
    
        // [START connect_to_fcm]
        func connectToFcm() {
            FIRMessaging.messaging().connectWithCompletion { (error) in
                if (error != nil) {
                    print("Unable to connect with FCM. \(error)")
                } else {
                    print("Connected to FCM.")
                }
            }
        }
        // [END connect_to_fcm]
    
        func applicationDidBecomeActive(application: UIApplication) {
            connectToFcm()
            UIApplication.sharedApplication().applicationIconBadgeNumber = 0
            //FBSDKAppEvents.activateApp()
        }
    
        // [START disconnect_from_fcm]
        func applicationDidEnterBackground(application: UIApplication) {
            //FIRMessaging.messaging().disconnect()
            print("Disconnected from FCM.")
        }
        // [END disconnect_from_fcm]
    
    
        //###################################### FIREBASE ##########################################
        //######################################## END #############################################
    }
    

    Payload Format:

    [aps: {
        alert =     {
            body = "Some message.";
            title = "Some title";
         };
         category = " ";
     }, Name: ios, gcm.message_id: 0:1474608925388897%17bce75117bc5555]
    
    0 讨论(0)
提交回复
热议问题