php 带有发送自定义数据的 Apple 推送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8134155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:01:59  来源:igfitidea点击:

Apple Push Notification with Sending Custom Data

phpiphoneapple-push-notifications

提问by vikas

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.

我正在将推送通知从 php 工作申请发送到 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 ?

我的意思是我想知道我可以发送任何带有推送通知的自定义数据,例如 jobId,其他东西....这样 Iphone 端可以检索并显示特定的工作吗?

Thanks.

谢谢。

回答by typeoneerror

Regardless of the language and library you use, the push notification payload is a JSON payload:

无论您使用何种语言和库,推送通知负载都是 JSON 负载:

{
    "aps": {
         "badge": 10,
         "alert": "Hello world!",
         "sound": "cat.caf"
    }
}

The apstoken is the Apple APN data. You can add custom data to your payload as well:

aps令牌是苹果APN数据。您也可以将自定义数据添加到负载中:

{
    "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];
    }
}

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.

请记住,有效负载的总大小为 256字节,这当然包括您的自定义参数。所以你可能不得不(冒着降低可读性的风险)调用你的自定义参数“ji”而不是“job_id”来压缩字节。

All of this is documented in the Local and Push Notification Programming Guidein the iOS documentation. Definitely would recommend a read because it's more complex than it initially sounds (at least, that's what I thought).

所有这些都记录在iOS 文档的本地和推送通知编程指南中。绝对会推荐阅读,因为它比最初听起来更复杂(至少,我是这么认为的)。

回答by Arfeen

Yes you can send custom data, check apns-php libraryfor all push notification needs:

是的,您可以发送自定义数据,请检查apns-php 库以了解所有推送通知需求:

回答by alex.wang

We can add more than one custom data,but, if i use sub_actionas the name, my iPhone can't receive push msg

我们可以添加多个自定义数据,但是,如果我将其sub_action用作名称,我的 iPhone 将无法接收推送消息

Java:

爪哇:

PayLoad payLoad = new PayLoad();
payLoad.addCustomDictionary("action", action_type);
payLoad.addCustomDictionary("subaction", sub_action_type);