UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Push Notification not working

    Scheduled Pinned Locked Moved App Development
    12 Posts 7 Posters 825 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      This topic has been deleted. Only users with topic management privileges can see it.
      • beli3verB Offline
        beli3ver
        last edited by

        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?

        beli3verB 1 Reply Last reply Reply Quote 0
        • beli3verB Offline
          beli3ver @beli3ver
          last edited by

          Maybe you @Krille

          ? 1 Reply Last reply Reply Quote 0
          • ? Offline
            A Former User @beli3ver
            last edited by

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

            arubislanderA 1 Reply Last reply Reply Quote 0
            • arubislanderA Offline
              arubislander @Guest
              last edited by

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

              πŸ‡¦πŸ‡Ό πŸ‡³πŸ‡± πŸ‡ΊπŸ‡Έ πŸ‡ͺπŸ‡Έ
              Happily running Ubuntu Touch
              Google Pixel 3a (20.04 DEV)
              JingPad (24.04 preview)
              Meizu Pro 5 (16.04 DEV)

              1 Reply Last reply Reply Quote 0
              • K Offline
                kugiigi
                last edited by

                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

                1 Reply Last reply Reply Quote 0
                • beli3verB Offline
                  beli3ver
                  last edited by

                  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 πŸ˜†

                  1 Reply Last reply Reply Quote 0
                  • beli3verB Offline
                    beli3ver
                    last edited by

                    @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?

                    K 1 Reply Last reply Reply Quote 0
                    • K Offline
                      kugiigi @beli3ver
                      last edited by

                      @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.

                      poVoqP 1 Reply Last reply Reply Quote 0
                      • AppLeeA Offline
                        AppLee
                        last edited by

                        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

                        1 Reply Last reply Reply Quote 0
                        • poVoqP Offline
                          poVoq @kugiigi
                          last edited by

                          @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.

                          Fairphone 5 (waiting for port)

                          K 1 Reply Last reply Reply Quote 0
                          • K Offline
                            kugiigi @poVoq
                            last edited by

                            @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.

                            1 Reply Last reply Reply Quote 2
                            • D Offline
                              domubpkm
                              last edited by

                              @beli3ver your notification system works well πŸ‘

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post