如何在AppDelegate之外放置FCM的通知授权请求?

时间:2018-04-08 22:44:26

标签: ios swift firebase firebase-cloud-messaging

当我们要实施Firebase云消息传递时,我们首先要求向用户发出通知权限。从Firebase文档中,他们建议在application上的Appdelegate中实现这一行代码,如下所示:

application

但是如果将这些代码放在appDelegate中,则在用户打开我的应用程序后会立即显示询问权限的弹出警报。但我想在某个View Controller中显示警报。

如果我将这些代码移到VC,则didFinishLaunchingWithOptions不可用,因为int readImage() { std::auto_ptr<FITS> pInfile(new FITS("atestfil.fit",Read,true)); PHDU& image = pInfile->pHDU(); std::valarray<unsigned long> contents; // read all user-specifed, coordinate, and checksum keys in the image image.readAllKeys(); image.read(contents); // !!!ERROR HERE!!! // this doesn’t print the data, just header info. std::cout << image << std::endl; long ax1(image.axis(0)); long ax2(image.axis(1)); for (long j = 0; j < ax2; j+=10) { std::ostream_iterator<short> c(std::cout,"\t"); std::copy(&contents[j*ax1], &contents[(j+1)*ax1-1], c); std::cout << '\n'; { std::cout << std::endl; return 0; } 来自undefined reference to `void CCfits::PHDU::read<unsigned long>(std::valarray<unsigned long>&)' 。然后是messagingDelegate ......我不确定。你可以帮我改写那些代码,但是如果它不在AppDelegate中吗?

2 个答案:

答案 0 :(得分:0)

添加课程

import Foundation
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications

class Notifications {

    private init() { }
    static let shared = Notifications()

    func requestNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isAuthorized, error) in
            if error != nil {
                print(error as Any)
                return
            } else {
                print("set up successfully")
                // Completion Code Here:
            }
        }
    }
}

添加到您首选的VC

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        Notifications.shared.requestNotifications()
    }

答案 1 :(得分:0)

我正在iOS 10.3.3设备上进行测试,直到我将UNUserNotificationCenter的委托设置为UIApplication.shared.delegate as? UNUserNotificationCenterDelegate并在AppDelegate中实现为空委托的委托后,它才注册。但是在当前的视图控制器中实现空委托是行不通的。

AppDelegate.swift:

extension AppDelegate: UNUserNotificationCenterDelegate { }

这是我的视图控制器中的方法:

private func registerPushNotifications() {
    Messaging.messaging().delegate = self
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as? UNUserNotificationCenterDelegate

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {granted, _ in
                if granted {
                    DispatchQueue.main.async {
                        UIApplication.shared.registerForRemoteNotifications()
                    }
                }
        })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
        UIApplication.shared.registerForRemoteNotifications()
    }
}

当然,您必须使用FirebaseApp.configure()方法离开didFinishLaunchingWithOptions

以后的编辑:您可能会收到有关从主线程调用registerForRemoteNotifications()的警告,因此我调整了代码以实现此目的。