【问题标题】:How to display message preview in push notification instead of title如何在推送通知而不是标题中显示消息预览
【发布时间】:2021-07-09 11:12:58
【问题描述】:

我正在尝试在通知横幅中显示消息预览,当前显示标题,并希望显示“文本”,但找不到任何相关信息..

有效负载格式

["badge": 1, "alert": {
    body =     {
        Body =         {
            FromInside = 1;
            MessageId = "123456";
            Read = 0;
            Status = 0;
            Text = Hi, hows it going?;
            Timestamp = "2021-07-09T08:37:10.5020307Z";
        };
        PHONE = 07123456789;
        CODE = 123456;
        "_id" = 000000000000000000000000;
    };
    time = "7/9/2021 8:37:10 AM";
    title = "New message";
}, "sound": default]

用户通知中心

    func userNotificationCenter(
      _ center: UNUserNotificationCenter,
      didReceive response: UNNotificationResponse,
      withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        let aps = userInfo["aps"] as? [String: Any]
        let alert = aps!["alert"] as? [String: Any]
        let messageBody = alert!["body"] as? [String: Any]  
        if UIApplication.shared.applicationState == .background || UIApplication.shared.applicationState == .inactive {

//            let body = messageBody!["Body"] as? [String: Any] ?? nil
//            let text = body!["Text"] as? String ?? ""
            
            UIApplication.shared.applicationIconBadgeNumber = 0
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "newMessage"), object: nil, userInfo:messageBody)
        }
        completionHandler()
    }

【问题讨论】:

  • 到底是什么问题?通知横幅显示“新消息”作为其标题,但您希望它显示其他文本?您能否包含您要发送 APNS 端点的原始 JSON?

标签: swift xcode apple-push-notifications


【解决方案1】:

userNotificationCenter 方法根本不会影响您的远程通知。如果您想更改通知的标题,则需要在发送到 APNS 端点的 JSON 中进行更改。

See this table for the JSON format.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多