应用程序在后台时如何阅读通知?

时间:2018-11-12 09:32:25

标签: ios swift push-notification

我正在使用swift 3,并尝试读取public class SeleniumDriverSetup extends Thread { public static WebDriver getDriver(String strBrowser) throws Exception { WebDriver driver = null; ExcelUtils.setExcelFile(HelperMethods.getRunEngineFileName()); String OS = GlobalConstants.operatingSystem; if(OS.equalsIgnoreCase("Windows")){ if(strBrowser.trim().toUpperCase().equals("CHROME")) { System.setProperty("webdriver.chrome.driver",GlobalConstants.librariesPath +"chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); System.out.println("***CHROME DRIVER HAS BEEN CREATED"); } } return driver; } } 中的通知

didReceiveRemoteNotification

在应用程序运行时可以正常工作,但在应用程序处于后台(不活动)时不打印任何内容。应用程序在后台(不活动)时如何阅读通知。

1 个答案:

答案 0 :(得分:0)

在为您的应用程序中的后台通知获取适当的权限并在适当的viewController中采用UNUserNotificationCenterDelegate之后。您可以实现此方法以在后台触发通知,并采取措施以执行通知中的任何措施。要从通知中执行任何操作,您需要采用功能userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

func startNotifications() {




        let notificationCenter = UNUserNotificationCenter.current()

// to include any action in the notification otherwise ignore it.

        let action = UNNotificationAction(identifier: "ActionRelatedIdentifier", title: "Name which you want to give", options: [.destructive, .authenticationRequired])

        let category = UNNotificationCategory(identifier: "CategoryName", actions: [stopAction], intentIdentifiers: [],  options: [])

        notificationCenter.setNotificationCategories([category])

        let content = UNMutableNotificationContent()
        content.title = "Your Title"
        content.body = "Message you want to give"
        content.sound = UNNotificationSound.default



      // To trigger the notifications timely and repeating it or not  
           let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.5, repeats: false)





        content.categoryIdentifier = "CategoryName"



            print("Notifications Started")

            let request = UNNotificationRequest(identifier: "NotificationRequestIdentifier", content: content, trigger: trigger)

            notificationCenter.add(request, withCompletionHandler: { (error) in
                if error != nil {
                    print("Error in notification : \(error)")
                }
            })


    }

免责声明:请根据您的需要应用解决方案,并询问是否发生任何错误。该代码用于触发后台通知。