如何在 swift 中为推送通知设置 AppDelegate

how to setting up AppDelegate for push notification in swift

我正在尝试为我的应用程序设置推送通知系统。我有一个服务器和一个开发人员许可证来设置推送通知服务。

我目前 运行 我的应用在 Swift4 Xcode 9

这是我的问题:

1_ 可以设置通知消息的标题和body吗??

2_接受按摩有什么作用?我正在使用 didReceiveRemoteNotification 但是当我触摸通知时会调用它 我需要一个函数 在显示我可以在其上设置按摩的通知之前调用它

3_ 我在 appDelegate 和我的服务器登录页面中生成设备令牌,它们彼此不同。这不正确吧?

这是我的应用委托:

          func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
                // Override point for customization after application launch.

                print("lunch",launchOptions?.description,launchOptions?.first)

                 application.registerForRemoteNotifications()
                FirebaseApp.configure()
                GMSPlacesClient.provideAPIKey("AIzaSyAXGsvzqyN3ArpWuycvQ5GS5weLtptWt14")

                UserDefaults.standard.set(["fa_IR"], forKey: "AppleLanguages")
                UserDefaults.standard.synchronize()


                  registerForPushNotifications()
                return true
            }


       func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {

            print("test : ",messaging.apnsToken)
        }

        func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

            print("Recived: \(userInfo)")
            print()
           // completionHandler(.newData)

        }


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        print("userInfo : ",userInfo)
        if application.applicationState == .active {
            print("active")
            //write your code here when app is in foreground
        } else {
            print("inactive")

            //write your code here for other state
        }
    }


   func getNotificationSettings() {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().getNotificationSettings { (settings) in
                print("Notification settings: \(settings)")
                guard settings.authorizationStatus == .authorized else { return }
                DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
                }
            }
        } else {


        }
    }


    func registerForPushNotifications() {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
                (granted, error) in
                print("Permission granted: \(granted)")
                guard granted else { return }
                self.getNotificationSettings()
            }
        } else {

            let settings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
            UIApplication.shared.registerForRemoteNotifications()


           // self.getNotificationSettings()
        }
    }
  1. 是的,您可以通过在通知中发送适当的负载来管理通知的内容。以以下模式发送有效负载将在通知
  2. 中显示标题和 body
{
        "aps" : {
            "alert" : {
                "title" : "Game Request",
                "body" : "Bob wants to play poker",
            },
            "badge" : 5
        } 
}
  1. 显示通知由系统根据应用程序状态处理。如果应用程序处于前台状态,您将在 didReceiveRemoteNotification 中收到调用,否则,系统会处理显示部分并在用户点击通知时在应用程序中获得控制权。

您无法从应用程序端编辑通知内容。

  1. 根据文档

APNs can issue a new device token for a variety of reasons:

User installs your app on a new device

User restores device from a backup

User reinstalls the operating system

Other system-defined events

因此建议在启动时请求设备令牌。

您可以在登录页面发送令牌,而不是在登录页面请求新令牌。