简体   繁体   English

如何快速设置AppDelegate的推送通知

[英]how to setting up AppDelegate for push notification in swift

I am trying to setup a push notification system for my application. 我正在尝试为我的应用程序设置推送通知系统。 I have a server and a developer license to setup the push notification service. 我具有服务器和开发人员许可证,可以设置推送通知服务。

I am currently running my app in Swift4 Xcode 9 我目前在Swift4 Xcode 9中运行我的应用

here are my questions : 这是我的问题:

1_ is that possible that I set the title and body of notification massage ?? 1_是否可以设置通知消息的标题和正文?

2_ what is the func of receiving massage ? 2_接受按摩的功能是什么? I'm using didReceiveRemoteNotification but this is called when I touch the notification I need a func which is called before showing notification that I can set my massage on it 我正在使用didReceiveRemoteNotification但是当我触摸通知时会调用该函数,我需要一个函数,该函数在显示可以设置按摩功能的通知之前被调用

3_ I'm generating device token in appDelegate and also in my login page for my server which are different from each other. 3_我正在appDelegate和服务器的登录页面中生成彼此不同的设备令牌。 this is not correct right ? 这是不正确的吧?

this is my app delegate : 这是我的应用程序委托:

          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. Yes, you can manage the content of notification by sending an appropriate payload in the notification. 是的,您可以通过在通知中发送适当的有效负载来管理通知的内容。 Sending the payload in the following pattern would show title and body in the notification 按照以下模式发送有效载荷将在通知中显示标题和正文
 { "aps" : { "alert" : { "title" : "Game Request", "body" : "Bob wants to play poker", }, "badge" : 5 } } 
  1. Display the notification is handled by the system depending upon the app state. 显示通知是由系统根据应用程序状态处理的。 If the app is the foreground state you will get the call in the didReceiveRemoteNotification , otherwise, the system handles the displaying part and get control in the app when the user taps on the notification. 如果应用程序处于前台状态,则将在didReceiveRemoteNotification获得呼叫,否则,当用户点击通知时,系统将处理显示部分并获得应用程序中的控制权。

You cannot edit the content of notification from the app side. 您无法从应用程序一侧编辑通知的内容。

  1. According to the document 根据文件

APNs can issue a new device token for a variety of reasons: APN可以出于多种原因发行新的设备令牌:

User installs your app on a new device 用户在新设备上安装您的应用

User restores device from a backup 用户从备份中还原设备

User reinstalls the operating system 用户重新安装操作系统

Other system-defined events 其他系统定义的事件

So its recommended requesting device token at launch time. 因此,建议在启动时请求设备令牌。

You can send the token in login page rather than requesting a new token in the login. 您可以在登录页面中发送令牌,而无需在登录名中请求新令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在推送通知Swift 2 AppDelegate上加载特定的viewController - Load a specific viewController on push notification Swift 2 AppDelegate Swift with Parse:向appDelegate添加推送通知支持时出错 - Swift with Parse: Error when adding push notification support to appDelegate 在appDelegate上通过swift接收远程通知后,关闭视图并推送(或performSegueWithIdentifier)? - DismissView and push (or performSegueWithIdentifier) after Receive Remote Notification on appDelegate by swift? 使用Swift从推送通知appdelegate更改视图控制器中UIWebView的URL - change URL of UIWebView in view controller from push notification appdelegate with Swift Swift-在AppDelegate.swift中设置依赖注入 - Swift - Setting up Dependency Injection in AppDelegate.swift Swift 3:如何在AppDelegate中设置UINavigationBar setBackgroundImage - Swift 3: How to set up UINavigationBar setBackgroundImage in AppDelegate 在AppDelegate中使用推送通知重定向视图 - Redirect a View with Push Notification in AppDelegate 如何在AppDelegate的推送通知中添加默认声音 - How to add the default sound to my push notification in AppDelegate 如何处理 swift 中的推送通知? - How to handle push notification in swift? Swift:在 AppDelegate 中设置标签文本 - Swift: Setting label text in the AppDelegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM