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

How to send a silent Push Notification payload

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

这是我发给客户的aps

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

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

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

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

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

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

然后在您的 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;
    }


}

希望这能解决您的问题!

有几种选择!让我们来了解一下所有不同的有效负载及其用法。


简单负载

在通知中心显示:是

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

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

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

在通知中心显示:是

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

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

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

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

带有自定义负载的通知

在通知中心显示:是

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

{
    "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"
    },

}

这里的 data 字典包含您想要的任何自定义信息。它还将显示为带有警报消息 "It's a notification with custom payload!".

的正常通知

普通静音通知

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

在通知中心显示:否

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

{
    "content-available" : 1
}

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

魔术来了,可以显示通知提醒并在后台唤醒您的应用程序以执行任务! (注意:仅当它在后台处于 运行 且未被用户明确杀死时。) 只需在您的负载中添加额外参数 "content-available" : 1

在通知中心显示:是

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

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

根据您的应用要求使用这些负载中的任何一个。 background app refresh 参考 Apple's documentation。我希望这能为您提供所有必要的信息。编码愉快:)

请同时检查apns-push-type(watchOS 6 及更高版本需要;推荐用于 macOS、iOS、tvOS 和 iPadOS)此 header 的值必须准确反映内容您的通知的有效负载。如果存在不匹配,或者如果 header 在所需系统上缺失,APNs 可能 return 出错,延迟通知的传递,或者完全丢弃它。 https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns