极光推送-IOS如何设置标题和子标题

2022-09-23

1.效果展示介绍

iOS 10 以下支持设置标题 title ,设置后 title 值将取代通知栏上的应用名称:
067358a883e05b955f5233e7fe3d55f3fa512ac3
iOS 10 及以上支持设置标题 title 和 subtitle,title 和 subtitle 自带加粗效果,无法取消,展示效果如下图:
9565fd83db15682a0e87950a0d61dfee49e126ba

2.官网推送传值介绍

选择推送通知 → 选择 iOS 目标平台 → 展开可选设置 → iOS 可选设置下有 title 和 subtitle 选择
7b91f0726eb0942fc5cc1bc31626b62f4aab62b4

3.push api推送传值介绍

IOS的alert字段可以设置String或者JSON Object,需要设置标题和子标题,需要以JSON Object的形式传入,具体可参考:
https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification

{
    "notification" : {
         "ios" : {
                 "alert" : {
                     "title" : "JPush Title", //可选设置
                     "subtitle" : "JPush Subtitle" , //可选设置
                     "body" : "JPush Body" //必填,否则通知将不展示,在不设置 title 和 subtitle 时直接对 alert 传值即可,不需要特地写 body 字段
                 }, 
                 "sound" : "sound.caf", 
                 "badge" : 1, 
                 "extras" : {
                      "news_id" : 134, 
                      "my_key" : "a value"
                 }
            }
       }
}

7b91f0726eb0942fc5cc1bc31626b62f4aab62b4-1663920205180
苹果官方:https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW5

4.代码示例

1、Java SDK

public static void testSendIosAlert() {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        IosAlert alert = IosAlert.newBuilder()
                .setTitleAndBody("test alert", "subtitle", "test ios alert json")
                .setActionLocKey("PLAY")
                .build();
        try {
            PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");
            LOG.info("Got result - " + result);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
    }
                
                                
补充:                
 //设置IOS通知消息
.setNotification(Notification.newBuilder()
//设置ios通知消息
.addPlatformNotification(IosNotification.newBuilder()
 //设置ios标题
.setAlert(IosAlert.newBuilder()
.setTitleAndBody("title", "subtitle", "test ios alert json")
.build()).build())

2、PHP SDK

try {
    $response = $client->push()
        ->setPlatform(array('ios', 'android'))
        ->addRegistrationId($registration_id)
        ->setNotificationAlert('Hi, JPush')
        ->iosNotification(array(
                      'title' => 'title', //可选设置
                      'subtitle' => 'subtitle', //可选设置
                      'body' => 'body'//必填,否则通知栏不展示
            ),array(
            'sound' => 'sound.caf',
            'extras' => array(
                'key' => 'value',
                'jiguang'
            ),
        ))
        ->options(array(
            // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境
            'apns_production' => false,
        ))
        ->send();
        print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
    // try something here
    print $e;
} catch (\JPush\Exceptions\APIRequestException $e) {
    // try something here
    print $e;
}