iOS 本地通知和日历事件不会出现,而应用程序不是 运行

iOS local notification and calendar event does not appear while app is not running

我们想在我们的项目中实现静默推送,但遇到一个奇怪的问题!

首先,有一个演示项目,在我的程序环境中运行良好, 但是在我将相同的代码应用到我的其他项目之后,当应用程序与 xcode/in 背景断开连接时,在 AppDelegate 中收到静默推送通知(神奇的东西)后,本地通知不会出现,并且有关于日历的计划 B,但是和上面一样,所以,我没有解决的办法,有什么建议吗?

1。静默推送通知和本地通知演示项目

 * xcode 10.1, deployment target iOS 10.0

 * objective-c 
 * reference tutorial: https://medium.com/@mikru168/ios-%E6%9C%AC%E5%9C%B0%E9%80%9A%E7%9F%A5-local-notification-b25229f279ec

--------> 很有魅力。收到静默推送通知后出现本地通知。

2。我的项目+“1.”(以上代码)

* xcode 10.1, iOS 9.0
* objective-c mix swift 4.0

--------> 在调试时工作正常(连接 xcode),在应用程序处于前台和后台时均如此。

-------->(与 xcode 断开连接)应用程序在后台或终止时不会出现本地通知,但在前台工作正常。

3。我的项目 + 日历事件

--------> 在调试时工作正常(连接 xcode),在应用程序处于前台和后台时均如此。

-------->(与 xcode 断开连接)当应用程序在后台或终止时日历通知不会出现,但在前台工作正常。

我试过你给的演示,我只是添加了一些新的方法和代码。 当应用程序与 Xcode.

断开连接时,它对我有用

请尝试将以下方法添加到您的项目中并进行测试。

首先,请检查您的项目中是否启用了以下服务

  1. 后台模式 > 远程通知
  2. 推送通知

其次,我在 didFinishLaunchingWithOptions

中添加了这个方法
application.registerForRemoteNotifications()

此外,在 appdelegate class

中添加了以下方法
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
     self.addLocalNotification()
    completionHandler(.newData)
}
func addLocalNotification(){
    let content = UNMutableNotificationContent()
    content.title = "title:Test"
    content.subtitle = "subtitle:No---"
    content.body = "body:Silent Notification"
    content.badge = 1
    content.sound = UNNotificationSound.default()

    // 設置通知的圖片
    let imageURL = Bundle.main.url(forResource: "apple", withExtension: "png")
    let attachment = try! UNNotificationAttachment(identifier: "", url: imageURL!, options: nil)
    content.attachments = [attachment]

    // 設置點擊通知後取得的資訊
    content.userInfo = ["link" : "https://www.facebook.com/franksIosApp/"]

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)

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

    UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
        print("成功建立通知...")
    })
}

我想和像我一样遇到这个问题的人分享, 这是 Apple 的故障排除,它真的很有帮助: https://developer.apple.com/library/archive/technotes/tn2265/_index.html

我最后的解决办法是触发时间超过60s