菜鸟教程小白 发表于 2022-12-11 19:06:51

ios - 推送通知仅在使用 Xcode 启动应用程序时有效


                                            <p><p>我有一个大问题,我在我的 iOS 项目上设置了(非常困难)推送通知。我决定在“AppDelegate”的“didReceiveRemoteNotification”方法中接收通知数据,然后以编程方式创建它(以执行强制性个人处理)。一切正常,只有当我在没有 Xcode 帮助的情况下启动我的应用程序时,我不再收到通知,通知创建代码没有执行。我不知道该怎么办......</p>

<pre><code>func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: ,
               fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -&gt; Void) {

    let body = userInfo[&#34;body&#34;]

    print(userInfo)

    if #available(iOS 10.0, *) {
      let content = UNMutableNotificationContent()
      content.body = body! as! String
      let trigger = UNTimeIntervalNotificationTrigger(timeInterval:
1, repeats: false)

      let request = UNNotificationRequest(identifier: &#34;idt&#34;, content:
content, trigger: trigger)
      UNUserNotificationCenter.current().add(request,
withCompletionHandler: nil)

    } else {
      // Fallback on earlier versions
    }

    completionHandler(UIBackgroundFetchResult.newData)
}
</code></pre>

<p>非常感谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您从通知启动应用时,您需要检查 <code>application:didFinishLaunchingWithOptions:</code> 中的 <em>launchOptions</em> 以查看 <code>UIApplicationLaunchOptionsRemoteNotificationKey</code> 是否存在。 </p>

<p>根据<a href="https://developer.apple.com/documentation/uikit/uiapplicationlaunchoptionsremotenotificationkey?language=objc" rel="noreferrer noopener nofollow">Documentation</a> :</p>

<blockquote>
<p>The value of this key is an NSDictionary containing the payload of the remote notification. See the description of application:didReceiveRemoteNotification: for further information about handling remote notifications.
This key is also used to access the same value in the userInfo dictionary of the notification named UIApplicationDidFinishLaunchingNotification.</p>
</blockquote>

<p>一旦您确定启动包含通知,请使用从 <em>launchOptions</em> 获得的 <em>通知负载</em> 调用通知处理程序方法。</p>

<p> <a href="https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application" rel="noreferrer noopener nofollow">What you have now</a> ,即 <code>application(_:didReceiveRemoteNotification:)</code> 仅在应用已经运行时触发:</p>

<blockquote>
<p>If the app is running, the app calls this method to process incoming remote notifications. The userInfo dictionary contains the aps key whose value is another dictionary with the remaining notification </p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 推送通知仅在使用 Xcode 启动应用程序时有效,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46957318/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46957318/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 推送通知仅在使用 Xcode 启动应用程序时有效