Search code examples
web-applicationspush-notificationpushtizen

Custom Tizen push message


I am developing the Tizen web application and I used the Tizen push API. I successfully received the alert message from the Tizen push server, but how would like to custom the push message. I used the code below

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

How could I custom the push message?


Solution

  • I've prepared two working scenarios with push server and Galaxy Watch by following below points.

    Handling push message inside running web application

    1. I've created very simple application and applied these prerequisites
    2. Inside my application I've created directory shared/res with test.jpg file (my test icon for this scenario)
    3. I've prepared web-based code for sending push notifications to my testing device (please notice the message I've used to add custom icon):
    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. I've started my application in debug mode, and using below code I registered application for push notifications and send message:
        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. After a while, notificationCallback raises and shows console log with received data:

    A notification arrives: {"alertMessage":"Test application message","appData":"","date":"2021-01-19T10:23:11.000Z","message":"action=ALERT&imageTypeIcon=test.jpg&alertMessage=Test application message","requestId":"000001","sender":"","sessionInfo":""}

    1. This is the end of basic scenario - in this case you need to handle message inside your application.

    Background scenario - push message handled automatically by service

    Second scenario is when the application is in background and push message is handled automatically by push service. Points 1-3 are the same. After that you need to move your application into background and continue with point 4. After a while customized notification with test.jpg icon should be shown as a system notification.