Notification - FCM 설정(3) : 소스 및 테스트

2017. 2. 2. 13:15Programming/Swift

반응형

3. 소스 확인 및 테스트


 - 이제 거의 다 왔다.

 - firebase 에 나와 있는 소스는 swift3과 다른 부분이 있어서, 변경된 부분만 stackoverflow에서 참고하여 수정하였다.

(1) 앱에서 Firebase 초기화

(2) 소스

(3) 테스트 


(1) 앱에서 Firebase 초기화


   - AppDelegate.swift 파일에 추가



(2) 소스

 - AppDelegate.swift

import UIKit

import Firebase

import UserNotifications


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {


    var window: UIWindow?

    let token = FIRInstanceID.instanceID().token()


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

        // Override point for customization after application launch.

        

        FIRApp.configure()

        

        let center = UNUserNotificationCenter.current()

        center.delegate = self

        center.requestAuthorization(options: [.sound,.alert,.badge]) { (granted, error) in

            // Enable or disable features based on authorization

            

        }

        application.registerForRemoteNotifications()

        

        return true

    }


/*    

    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()

    }

    

    func connectToFcm() {

        FIRMessaging.messaging().connect { (error) in

            if (error != nil) {

                print("Unable to connect with FCM. \(error)")

            } else {

                print("Connected to FCM.")

            }

        }

    }

    

    func application(_ application: UIApplication,

                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.sandbox)

    }

    

    private func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],

                     fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

        // If you are receiving a notification message while your app is in the background,

        // this callback will not be fired till the user taps on the notification launching the application.

        // TODO: Handle data of notification

        

        // Print message ID.

        //print("Message ID: \(userInfo["gcm.message_id"]!)")

        

        // Print full message.

        print("%@", userInfo)

    }

*/ 

...

...

    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.

        

//        FIRMessaging.messaging().disconnect()

//        print("Disconnected from FCM.")

    }

...

...

}


(3) 테스트









반응형

'Programming > Swift' 카테고리의 다른 글

alertMessage 왼쪽 정렬  (0) 2017.02.15
3D 터치 구현  (0) 2017.02.02
Notification - FCM 설정(2) : 선행 조건  (0) 2017.02.02
Notification - FCM 설정(1) : 참고 URL  (0) 2017.02.01
swift3 Bool 기본값  (0) 2017.01.31