简体   繁体   English

如何处理未打开的推送通知(iOS,Swift)

[英]How to Handle Push Notifications that are not Opened (iOS, Swift)

So I am running a Swift application and using Parse to handle my push notifications. 所以我正在运行Swift应用程序并使用Parse来处理我的推送通知。 So far everything is working as expected. 到目前为止,一切都按预期工作。

I am sending JSON objects through the push notifications and updating a local array from the data received. 我通过推送通知发送JSON对象,并从收到的数据更新本地数组。 I am handling the data through the application: didReceiveRemoteNotification and sending a dictionary through userInfo . 我通过application: didReceiveRemoteNotification处理数据application: didReceiveRemoteNotification并通过userInfo发送字典。

This works perfectly when the app is open in the foreground. 当应用程序在前台打开时,这非常有效。 It also works great when the app is in the background and the user opens the Push Notification when it is displayed in the banner. 当应用程序在后台并且用户在横幅中显示推送通知时,它也可以正常工作。

The JSON object is handled according to the application: didReceiveRemoteNotification code. 根据application: didReceiveRemoteNotification处理JSON对象application: didReceiveRemoteNotification代码。

However, when the user does not open the notification, application: didReceiveRemoteNotification does not get run and my local array does not get updated. 但是,当用户未打开通知时, application: didReceiveRemoteNotification不会运行,并且我的本地数组不会更新。

How do I make sure every time a notification is sent the JSON object gets handled and updates my local array accordingly? 如何确保每次发送通知时都会处理JSON对象并相应地更新我的本地数组?

You can not get get response or JSON object from Notification until User click on it and either didReceiveRemoteNotification or didFinishLaunchingWithOptions method get invoke. 在用户单击它并且调用didReceiveRemoteNotificationdidFinishLaunchingWithOptions方法之前,您无法从通知中获取响应或JSON对象。

From ios8, you can set interactive notification, which is useful to interact direct from notification, without opening application in foreground. 从ios8,您可以设置交互式通知,这对于直接从通知进行交互非常有用,无需在前台打开应用程序。

HERE it the tutorial for interactive Notification. 这里它的教程互动的通知。

May this help you. 愿这对你有所帮助。

It is generally incorrect approach, you should not rely on push notifications, user may switch them off or due to some reasons they may not be delivered. 这通常是不正确的方法,您不应该依赖推送通知,用户可能会关闭它们或由于某些原因它们可能无法交付。 Instead you should get new data always from your server. 相反,您应该始终从服务器获取新数据。 Read from the Apple Push Notification Service Guide : 阅读Apple推送通知服务指南

Important : Delivery of notifications is a “best effort”, not guaranteed. 重要提示 :发送通知是“尽力而为”,不能保证。 It is not intended to deliver data to your app, only to notify the user that there is new data available 它不是为了向您的应用提供数据,而是为了通知用户有新数据可用

application:didReceiveRemoteNotification:fetchCompletionHandler: 

this method will call even if user will not tap or interact with notification. 即使用户不会点击或与通知交互,此方法也会调用。

When a remote notification arrives, the system calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method. 当远程通知到达时,系统调用application:didReceiveRemoteNotification:fetchCompletionHandler: method。 Notifications usually signal the availability of new information. 通知通常表示新信息的可用性。 In your app delegate method, you might begin downloading new data from a server so that you can update your app's data structures. 在您的app delegate方法中,您可能会开始从服务器下载新数据,以便您可以更新应用程序的数据结构。 You might also use the notification to update your user interface. 您还可以使用通知来更新用户界面。 -Apple Inc. -苹果公司。

The state of app must be in UIApplicationStateActive or UIApplicationStateBackground . 应用程序的状态必须位于UIApplicationStateActiveUIApplicationStateBackground

You can not do anything if state of application is UIApplicationStateInactive . 如果应用程序的状态是UIApplicationStateInactive则无法执行任何操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM