我在我的应用程序中使用 APNS,我想发送一些数据(当然小于 256 字节)。我的问题是我想通过推送通知发送数据而不向用户显示任何警报。我怎样才能做到这一点 ? 谢谢
最佳答案
在 aps 键中指定角标(Badge),并将您的数据放入其他键中。因此,如果您使用角标(Badge)编号,请重新计算正确的角标(Badge)编号,如果您不使用角标(Badge)编号,则只需发送 0。
来自苹果文档:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean. You should not include customer information as custom payload data. Instead, use it for such purposes as setting context (for the user interface) or internal metrics. For example, a custom payload value might be a conversation identifier for use by an instant-message client application or a timestamp identifying when the provider sent the notification. Any action associated with an alert message should not be destructive—for example, deleting data on the device.
但有一个问题 - 这只有在应用程序正在运行时才有效。
它的工作原理是 - 如果应用程序正在运行,您将通过 didReceiveRemoteNotification 回调以及有效负载数据获得通知。您可以在此处提取自定义数据。 但是,如果应用程序未运行,通常会显示警报,并且仅当用户点击应用程序时才会通知您的应用程序。由于您不想显示警报 - 当应用程序未运行时,您无法获取自定义负载数据。仔细想想,如果应用程序自行启动,对用户来说会非常可怕。 :)
关于ios - 通过 APNS 在有效负载中发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631352/