Push Notification not working



  • Good morning,

    I am currently trying to integrate push notification for the app "uAdBlock". For this I proceeded like in the Wiki.
    Now I have a token. To test if everything works, I wanted to send a test message with this snippet:

    var req = new XMLHttpRequest();
    req.open("post", "https://push.ubports.com/notify", true);
    req.setRequestHeader("Content-type", "application/json");
    req.onreadystatechange = function() {
        if ( req.readyState === XMLHttpRequest.DONE ) {
            console.log("✍ Answer from push service:", req.responseText)
            var ans = JSON.parse(req.responseText)
            if ( ans.error ) {
                console.log(ans.error)
                if ( ans.message ) {
                    console.log(ans.message)
                }
            }
        }
    }
    var approxExpire = new Date ()
    approxExpire.setUTCMinutes(approxExpire.getUTCMinutes()+10)
    req.send(JSON.stringify({
        "appid" : 'uadblock.mariogrip_uAdBlock',
        "expire_on": approxExpire.toISOString(),
        "token": 'TOKEN',
        "data": {
            "notification": {
                "card": {
                    "icon": "notification",
                    "summary": "Push Notification",
                    "body": "test",
                    "popup": true,
                    "persist": true
                },
                "vibrate": true,
                "sound": true
            }
        }
    }))
    

    But nothing happens and in the console the answer from the server is empty. Have I forgotten anything? Maybe someone can help me?



  • Maybe you @Krille



  • @beli3ver Not much of experience with C, but is that correct: "if ( req.readyState === XMLHttpRequest.DONE )"? Shouldn't be "==" ?



  • @C0n57an71n as the code snippet posted is JavaScript, "===" is correct.



  • You did not set the token to the actual token and you just put the string TOKEN.
    I compare to my Pesbuk code and I think that's the main difference.

    You can check here.
    https://github.com/kugiigi/pesbuk-app/blob/89c75d28330c956e2aada21bc5cbbe9b5cf2016a/pesbuk/Main.qml



  • Ok I wrote TOKEN to not post my real token.

    But I think I found my mistake. I guess that the code snippet must be in the app as well?

    Currently I'm running it on the server and thought that this way I send notifications to the client, so I guess that was my mistake 😆



  • @kugiigi maybe you can help me.
    I am really stuck.
    I have integrated Push Notifications as it is written in the Wiki. I also have a function that sends the token to my server, I get that too.

    Now the question, the function above is used to send a notification from my server to the Ubuntu server or does this code have to be called into the app because it gets called when the Ubuntu server sends a notification to the app?



  • @beli3ver Oh I see, I didn't think of that 😅

    My app executes this code because it's actually just a workaround for having push notification in the webapp. I have no other experience with push notification but I think the code should actually be executed in the server but the token has to be coming from the device that should receive the notification.



  • Just to provide some reference links.

    The wiki you mentionned is probably the official documentation:
    http://docs.ubports.com/en/latest/appdev/guides/pushnotifications.html

    And have you checked the example app "PushClient" ?
    https://github.com/ChristianPauly/pushclient



  • @kugiigi said in Push Notification not working:

    My app executes this code because it's actually just a workaround for having push notification in the webapp.

    Oh, could you explain a bit how that works? It is taking the web notifications and translating them to UT push? I have been scratching my head how to implement push notifications with a web-app.



  • @poVoq No not really, it's a pure hacky way. I simply injected JS codes to check the notification flags in the webpage so it needed to disable app suspension. Actually, it seems that it stopped working. Not sure if it's Facebook's fault or UT's.



  • @beli3ver your notification system works well 👍


Log in to reply