【问题标题】:Remote notifications from background来自后台的远程通知
【发布时间】:2018-10-22 01:17:30
【问题描述】:

我正在快速创建一个应用程序,它将使用UNNotificationAction 按钮。

我已经正确设置了userNotificationCenter,我可以在应用程序打开时正确调用didReceive...从这里我显示一个模式窗口。

问题是,当应用程序未在前台或后台运行时(用户尚未打开应用程序),当我检查 launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] 时,我无法让 didFinishLaunchingWithOptions 解析我的代码

当用户使用UNNotificationAction点击推送通知时重新打开应用程序时,是否有一种新的处理技术?

【问题讨论】:

    标签: ios swift apple-push-notifications apn


    【解决方案1】:

    didFinishLaunchingWithOptions中,当你发现因为通知而启动时,将自己设置为UNUserNotificationCenter的delegate并返回false。现在将调用您的 didReceive 实现。

    【讨论】:

    • 感谢您的回答。不过,我似乎无法触发它,我在远程通知检查中添加了 return false 。没有运气。
    • 从头开始。看起来当我点击推送通知时它工作得很好,但是当我点击操作项UNNotificationAction“Snooze”时,它根本不起作用。
    【解决方案2】:

    请检查下面的代码,希望对您有所帮助。

         @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
    
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
            application.registerForRemoteNotifications()
        }
    
    
    
       @available(iOS 10, *)
    
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
    
        // With swizzling disabled you must let Messaging know about the message, for Analytics
         Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
    
        // print(userInfo)
          completionHandler([.alert, .badge, .sound])
        // Change this to your preferred presentation option
       // completionHandler([])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        switch response.actionIdentifier {
        case "action1":
            print("Action First Tapped")
        case "action2":
            print("Action Second Tapped")
        default:
            break
        }
    
        // Print full message.
        print(userInfo)
        Messaging.messaging().appDidReceiveMessage(userInfo)
        completionHandler()
    }
    

    【讨论】:

    • 谢谢,但这不是我的问题。我的问题是从应用程序关闭时我无法点击我的 UNNotificationAction。当应用程序在前台时,它工作正常,当应用程序在后台时。但是当用户根本没有打开应用程序并且它没有运行时,我无法点击 UNNotificationAction 操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2021-02-01
    相关资源
    最近更新 更多