带有发送自定义数据的 Apple 推送通知
我正在从 php 求职申请向 iphone 发送推送通知.我正在发送有关新工作的推送通知.这可能是因为当用户点击弹出的推送通知视图时,用户重定向到设备中的特定作业.
I am sending push notifications from php job application to iphone. I am sending push notifications regarding new jobs. Is this possible that when user click on the view of push notification pop up , then user redirect to the particular job in the device.
我的意思是我想知道我是否可以发送任何带有推送通知的自定义数据,例如 jobId 或其他内容....以便 Iphone 端可以检索并显示特定的工作?
I mean I wanted to know can I send any custom data with push notification like jobId,something else....so that Iphone end Can retrieve and show the particular job ?
谢谢.
无论您使用何种语言和库,推送通知负载都是 JSON 负载:
Regardless of the language and library you use, the push notification payload is a JSON payload:
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
}
}
aps
令牌是 Apple APN 数据.您也可以向负载添加自定义数据:
The aps
token is the Apple APN data. You can add custom data to your payload as well:
{
"aps": {
"badge": 10,
"alert": "Hello world!",
"sound": "cat.caf"
},
"job_id": 1
}
当您在应用中收到通知时,请在通知字典中检查您的参数:
When you receive the notification in the app, check for your param in the notification dictionary:
- (void)handleBackgroundNotification:(NSDictionary *)notification
{
NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
NSMutableString *alert = [NSMutableString stringWithString:@""];
if ([aps objectForKey:@"alert"])
{
[alert appendString:(NSString *)[aps objectForKey:@"alert"]];
}
if ([notification objectForKey:@"job_id"])
{
// do something with job id
int jobID = [[notification objectForKey:@"job_id"] intValue];
}
}
请记住,有效负载的总大小为 256 字节,当然,这包括您的自定义参数.因此,您可能不得不(冒着降低可读性的风险)调用您的自定义参数ji"而不是job_id"来压缩字节.
Keep in mind that the total size of the payload is 256 bytes, and that includes, of course, your custom parameters. So you may have to (at risk of reducing readability) call your custom param "ji" instead of "job_id" to squeeze bytes.
所有这些都记录在 本地和推送iOS 文档中的通知编程指南.绝对会推荐阅读,因为它比最初听起来更复杂(至少,我是这么认为的).
All of this is documented in the Local and Push Notification Programming Guide in the iOS documentation. Definitely would recommend a read because it's more complex than it initially sounds (at least, that's what I thought).