ios 如何发送静默推送通知负载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36323903/
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-31 08:48:37  来源:igfitidea点击:

How to send a silent Push Notification payload

iosswiftpush-notificationapple-push-notifications

提问by Sydney Loteria

I just want to know how I can determine what action to do on a silent push:

我只想知道如何确定在静默推送时要执行的操作:

This is the apsthat I sent to the client:

这是aps我发给客户的:

"aps": {
    "content-available": 1
}

My problem now is when I add type: "Order_Update"to determine that the silent push is for the Order Update to display an alert notification.

我现在的问题是当我添加type: "Order_Update"确定静默推送是为了让订单更新显示警报通知。

回答by Dipen Panchasara

There are a few options for it! Let's take a small ride to understand all the different payloads and their usage.

有几个选择!让我们小跑一下,了解所有不同的有效载荷及其用法。



Simple Payload

简单有效载荷

Displayed in Notification Center : Yes

显示在通知中心:是

Wakes app to perform background task : No

唤醒应用程序以执行后台任务:否

{
    "aps" : {
        "alert" : "You received simple notification!",
        "badge" : 1,
        "sound" : "default"
    }
}


Payload With Custom Notification Sound

带有自定义通知声音的有效负载

Displayed in Notification Center : Yes

显示在通知中心:是

Wakes app to perform background task : No

唤醒应用程序以执行后台任务:否

Step 1: Add custom notification sound file (.wav or .aiff extensions only. e.g. notification.wav) in your app bundle.

Step 1:在您的应用程序包中添加自定义通知声音文件(仅限 .wav 或 .aiff 扩展名。例如 notification.wav)。

Step 2: Configure your payload as shown below to play your custom sound

Step 2:如下所示配置您的有效负载以播放您的自定义声音

{
    "aps" : {
        "alert" : "It's a custom notification sound!",
        "badge" : 1,
        "sound" : "notification.wav"
    }
}


Notification With Custom Payload

带有自定义负载的通知

Displayed in Notification Center : Yes

显示在通知中心:是

Wakes app to perform background task : No

唤醒应用程序以执行后台任务:否

{
    "aps" : {
        "alert" : "It's a notification with custom payload!",
        "badge" : 1,
        "content-available" : 0         
    },
    "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
    },

}

Here the datadictionary holds custom information whatever you want. It will also display as normal notification with the alert message "It's a notification with custom payload!".

在这里,data字典包含您想要的任何自定义信息。它还将显示为带有警报消息“这是带有自定义负载的通知!”的正常通知。



Normal Silent Notification

正常的静音通知

It will not a show an alert as a notification bar; it will only notify your app that there is some new data available, prompting the app to fetch new content.

它不会将警报显示为通知栏;它只会通知您的应用有一些新数据可用,提示应用获取新内容。

Displayed in Notification center : No

通知中心显示:否

Awake app to perform background task : Yes

唤醒应用程序以执行后台任务:是

{
    "content-available" : 1
}


Silent Notification With Custom Payload

带有自定义负载的静默通知

Here comes the magic to show a notification alert as well awake your app in background for a task! (Note: only if it's running in background and has not been killed explicitly by the user.) Just add the extra parameter "content-available" : 1in your payload.

这是显示通知警报以及在后台唤醒您的应用程序以执行任务的魔力!(注意:仅当它在后台运行并且未被用户明确杀死时。)只需"content-available" : 1在您的有效负载中添加额外的参数。

Displayed in Notification Center : Yes

显示在通知中心:是

Wakes app to perform background task : Yes

唤醒应用程序以执行后台任务:是

{
    "aps" : {
        "alert" : "Notification with custom payload!",
        "badge" : 1,
        "content-available" : 1
    },
     "data" :{
        "title" : "Game Request",
        "body" : "Bob wants to play poker",
        "action-loc-key" : "PLAY"
     }
}


Use any of these payloads according to your app requirements. For background app refreshrefer to Apple's documentation. I hope this gives you all the necessary information. Happy coding :)

根据您的应用程序要求使用任何这些负载。对于background app refresh参考苹果的文档。我希望这可以为您提供所有必要的信息。快乐编码:)

回答by gunjot singh

As i understand, you want extra data inside payload, so you can identify what push notification type is,or what action need to be handled.

据我了解,您需要在有效负载中添加额外数据,以便您可以确定推送通知类型是什么,或者需要处理什么操作。

For that edit your payload as:

为此,将您的有效负载编辑为:

 $body = array(
    'content-available' => 1,
    'sound' => ''
    );  

$payload = array();
$payload['aps'] = $body;
$payload['action'] = 'order_update';

Then in your iOS Code:

然后在你的 iOS 代码中:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{


    NSString *action = userInfo["action"];


    if([userInfo[@"aps"][@"content-available"] intValue]== 1 && [action isEqualToString:@"order_update") //order update notification
    {
        //handle Your Action here
        return;
    }


}

Hope this solves your problem!

希望这能解决您的问题!