自定义 Tizen 推送消息

Custom Tizen push message

我正在开发 Tizen Web 应用程序并且我使用了 Tizen 推送 API。 我已成功收到来自 Tizen 推送服务器的警报消息,但如何自定义推送消息。 我使用了下面的代码

"badgeOption=INCREASE&badgeNumber=1&action=ALERT&alertMessage=Hi"

如何自定义推送消息?

这里详细描述了推送消息可能的内容https://docs.tizen.org/application/native/guides/messaging/push-server/ 不幸的是,该指南适用于本机应用程序,但其中大部分应该很容易适用于所有配置文件。

如果您想通过添加图标、更改声音或添加快速操作来自定义推送消息的外观,请参阅装饰推送通知部分。

我已经按照以下几点准备了推送服务器和 Galaxy Watch 的两个工作场景。

在 运行 网络应用程序中处理推送消息

  1. 我创建了非常简单的应用程序并应用了这些 prerequisites
  2. 在我的应用程序中,我创建了包含 test.jpg 文件的目录 shared/res(此场景的测试图标)
  3. 我已经准备好用于向我的测试设备发送推送通知的基于 Web 的代码(请注意我用来添加自定义图标的消息):
function sendMessage(registeredId, msg) {
    if (registeredId == undefined || msg == undefined || msg == "") {
        console.err("error registering application!");
        return;
    }
    var appId = "<<<your application package id>>>";
    var sec = "<<<your application's push server secret code>>>";

    var request = new XMLHttpRequest();
    var data = {
        "regID": registeredId,
        "requestID": "000001",
        "message": "action=ALERT&imageTypeIcon=test.jpg&alertMessage="+msg
    };

    var idToUrlMap = {
        "00": "https://useast.push.samsungosp.com:8090/spp/pns/api/push",
        "02": "https://apsoutheast.push.samsungosp.com:8090/spp/pns/api/push",
        "03": "https://euwest.push.samsungosp.com:8090/spp/pns/api/push",
        "04": "https://apnortheast.push.samsungosp.com:8090/spp/pns/api/push",
        "05": "https://apkorea.push.samsungosp.com:8090/spp/pns/api/push",
        "06": "https://apchina.push.samsungosp.com.cn:8090/spp/pns/api/push",
        "50": "https://useast.gateway.push.samsungosp.com:8090/spp/pns/api/push",
        "52": "https://apsoutheast.gateway.push.samsungosp.com:8090/spp/pns/api/push",
        "53": "https://euwest.gateway.push.samsungosp.com:8090/spp/pns/api/push",
        "54": "https://apnortheast.gateway.push.samsungosp.com:8090/spp/pns/api/push",
        "55": "https://apkorea.gateway.push.samsungosp.com:8090/spp/pns/api/push",
        "56": "https://apchina.gateway.push.samsungosp.com.cn:8090/spp/pns/api/push"
    };

    var url = idToUrlMap[registeredId.substring(0,2)];
    request.open("POST", url, true);

    request.setRequestHeader("Content-Type", "application/json");
    request.setRequestHeader("appID", appId);
    request.setRequestHeader("appSecret", sec);
    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            console.log(request.responseText);
            console.log("Push Success");
        }
    };
    request.send(JSON.stringify(data));
}
  1. 我已经在调试模式下启动了我的应用程序,并使用以下代码注册了推送通知和发送消息的应用程序:
    function errorCallback(response) {
        console.log("The following error occurred: " + response.name);
    }

    function registerSuccessCallback(id) {
        console.log("Registration succeeded with id: " + id);
        sendMessage(id, message || "Test application message")
    }

    function stateChangeCallback(state) {
        console.log("The state is changed to: " + state);

        if (state == "UNREGISTERED") {
            tizen.push.register(registerSuccessCallback, errorCallback);
        } else {
            var id = tizen.push.getRegistrationId();
            sendMessage(id, message || "Test application message")
        }
    }

    function notificationCallback(notification) {
        console.log("A notification arrives: " + JSON.stringify(notification));
    }

    /* Connects to push service. */
    tizen.push.connect(stateChangeCallback, notificationCallback, errorCallback);
  1. 一段时间后,notificationCallback 引发并显示包含接收到的数据的控制台日志:

通知到达:{"alertMessage":"测试应用程序消息","appData":"","date":"2021-01-19T10:23:11.000Z"," message":"action=ALERT&imageTypeIcon=test.jpg&alertMessage=测试应用消息","re​​questId":"000001","sender":"","sessionInfo":""}

  1. 基本场景到此结束 - 在这种情况下,您需要在应用程序内部处理消息。

后台场景-服务自动处理的推送消息

第二种情况是当应用程序在后台并且推送消息由推送服务自动处理时。第 1-3 点是相同的。之后,您需要将应用程序移至后台并继续第 4 点。 一段时间后,带有 test.jpg 图标的自定义通知应显示为系统通知。