• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何提示用户接受 AppDelegate 之外的推送通知?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:15:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的应用加载到一个 View Controller 中,该 Controller 告诉用户为什么他们应该接受推送通知。直到他们按下此 View Controller 上的继续按钮后,我才希望提示他们接受推送通知。这意味着我需要在我的初始 View Controller 中的某处包含代码,让我们称之为 BeforePromptController。我正在使用 Firebase,以防万一。

这是我在 AppDelegate 中的工作代码(但是我想更改它,以便在用户按下 BeforePromptController 中的继续按钮后出现提示)。我试图在这里只包含 AppDelegate 的重要部分..

import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {

    var window: UIWindow?

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

        FIRApp.configure()

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

        return true
    }



    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if application.applicationState == UIApplicationState.active {

            var pushNotificationMessage = ""
            if let aps = userInfo["aps"] as? NSDictionary {
                if let alert = aps["alert"] as? NSDictionary {
                    if let message = alert["message"] as? NSString {
                        pushNotificationMessage = message as String
                    }
                } else if let alert = aps["alert"] as? NSString {
                    pushNotificationMessage = alert as String
                }
            }

            let notificationAlert = UIAlertController(title: nil, message: pushNotificationMessage, preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
            })
            defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: "titleTextColor")

            notificationAlert.addAction(defaultAction)
            self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil)
        }
    }
}



Best Answer-推荐答案


您可以将代码移到需要授权的 FIRApp.configure() 之后;您可以使用 UIApplication.shared

获取应用程序的引用

关于ios - 如何提示用户接受 AppDelegate 之外的推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815777/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap