来自后台的远程通知

时间:2018-10-22 01:17:31

标签: ios swift apple-push-notifications apn

我正在快速创建一个使用UNNotificationAction按钮的应用。

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

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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