func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { FIRApp.configure()
initFirebade(application: application) return true }
func initFirebade(application: UIApplication){ if #available(iOS 10.0, *) { let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self // For iOS 10 data message (sent via FCM) FIRMessaging.messaging().remoteMessageDelegate = self } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.firInstanceIDTokenRefresh, object: nil) connectToFcm() } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // set firebase apns token FIRInstanceID.instanceID().setAPNSToken(deviceToken as Data, type: FIRInstanceIDAPNSTokenType.sandbox) } func tokenRefreshNotification(_ notification: Notification) { if let refreshedToken = FIRInstanceID.instanceID().token() { print("InstanceID token: \(refreshedToken)") Utils.setFirebaseToken(firebaseToken: refreshedToken) FIRMessaging.messaging().subscribe(toTopic: "/topics/ledices")
} // 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("Firebase Unable to connect with FCM. \(error)") } else { print("Firebase Connected to FCM.") FIRMessaging.messaging().subscribe(toTopic: "/topics/ledices")
} } }
func application(_ application: UIApplication, didReceive notification: UILocalNotification) { print("Firebase message 1") print(notification) }
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { print("Firebase message 2") } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("Firebase message 3") }
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { print("Firebase register settings") } func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("Firebase background fetch") }
public static void sendMessage(String destinatary,String stMessage) throws IOException{ FCMSender sender=new FCMSender(SERVER_KEY); Notification notification = new Notification.Builder(null) .body("Server Test") .title("Test").build(); Message message = new Message.Builder() .collapseKey("message") .timeToLive(60) //.delayWhileIdle(true) .priority(Priority.HIGH) .addData("message", "Notification from Java application") .notification(notification) .build();
// Use the same token(or registration id) that was earlier// used to send the message to the client directly from// Firebase Console's Notification tab. System.out.println(destinatary); Result result = sender.send(message,destinatary,5); System.out.println("Error: "+result.getErrorCodeName());
System.out.println("Success: "+result.getSuccess()); System.out.println("Message Id: "+result.getMessageId()); }