Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. GCM push notifications android
Qt 6.12 LTS is out! See what's new in the release blog

GCM push notifications android

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
23 Posts 6 Posters 12.8k Views 2 Watching
  • 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.
  • ekkescornerE ekkescorner

    @Schluchti said in GCM push notifications android:

    @SGaist Many thanks for the links! Didn't know that such great projects exist :)

    I already implemented push notifiction for iOs and android. As it took me some time to implement it, I am thinking about writing it down to save other Qt developers time when implementing that stuff.

    great idea ;-)
    best would be a small example project at gitHub together with blogpost

    S Offline
    S Offline
    Schluchti
    wrote on last edited by
    #10

    @ekkescorner Yeah, that's what I was thinking about ;-). Hope to get the first article done by the end of this week. :)

    Want to read more about Qt?

    https://gympulsr.com/blog/qt/

    Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m.kuncevicius
      wrote on last edited by
      #11

      Thank you for answers guys! I knew before about V-Play, but I'm not interested in using it since it is not free. @Schluchti it would be great if you don't mind sharing your code!

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Schluchti
        wrote on last edited by
        #12

        First part is now available. https://gympulsr.com/blog/qt/2017/03/08/push-notification-ios-qt.html
        I hope it covers everything.

        Want to read more about Qt?

        https://gympulsr.com/blog/qt/

        Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

        ekkescornerE 1 Reply Last reply
        3
        • S Schluchti

          First part is now available. https://gympulsr.com/blog/qt/2017/03/08/push-notification-ios-qt.html
          I hope it covers everything.

          ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by
          #13

          @Schluchti great article - thx providing your experiences and sample app.

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Schluchti
            wrote on last edited by
            #14

            Second part is now done: https://gympulsr.com/blog/qt/2017/03/26/push-notification-android-qt.html

            Hope I didn't miss anything (implemented the notifications a while ago and now tried to remember as much as I could ;-)). If something is missing, please let me know.

            Want to read more about Qt?

            https://gympulsr.com/blog/qt/

            Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

            ekkescornerE A 2 Replies Last reply
            2
            • S Schluchti

              Second part is now done: https://gympulsr.com/blog/qt/2017/03/26/push-notification-android-qt.html

              Hope I didn't miss anything (implemented the notifications a while ago and now tried to remember as much as I could ;-)). If something is missing, please let me know.

              ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by
              #15

              @Schluchti cool - blog look great :)

              probably next 2 weeks I'll test it.

              just still optimizing all my apps for Qt 5.8

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps

              1 Reply Last reply
              2
              • S Schluchti

                Second part is now done: https://gympulsr.com/blog/qt/2017/03/26/push-notification-android-qt.html

                Hope I didn't miss anything (implemented the notifications a while ago and now tried to remember as much as I could ;-)). If something is missing, please let me know.

                A Offline
                A Offline
                Atr0n
                wrote on last edited by Atr0n
                #16
                This post is deleted!
                S 1 Reply Last reply
                0
                • A Atr0n

                  This post is deleted!

                  S Offline
                  S Offline
                  Schluchti
                  wrote on last edited by Schluchti
                  #17

                  @Atr0n thanks! :)

                  You are refering to the android blog post, right? Have you enabled Gradle in the project settings? I haven't seen this errorin particular, but some similar packaging errors when using Ant. In my experience it sometimes also helps to delete the whole build folder to enforce a clean rebuild.

                  edit: are you sure that error is the acutal reason why it failed?

                  Want to read more about Qt?

                  https://gympulsr.com/blog/qt/

                  Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                  A 1 Reply Last reply
                  2
                  • S Schluchti

                    @Atr0n thanks! :)

                    You are refering to the android blog post, right? Have you enabled Gradle in the project settings? I haven't seen this errorin particular, but some similar packaging errors when using Ant. In my experience it sometimes also helps to delete the whole build folder to enforce a clean rebuild.

                    edit: are you sure that error is the acutal reason why it failed?

                    A Offline
                    A Offline
                    Atr0n
                    wrote on last edited by Atr0n
                    #18

                    @Schluchti said in GCM push notifications android:

                    @Atr0n thanks! :)

                    You are refering to the android blog post, right? Have you enabled Gradle in the project settings? I haven't seen this errorin particular, but some similar packaging errors when using Ant. In my experience it sometimes also helps to delete the whole build folder to enforce a clean rebuild.

                    edit: are you sure that error is the acutal reason why it failed?

                    Hi @Schluchti,
                    I deleted my last post because after some investigations, I realiced Qt creator has a bug which makes impossible to use the last gradle version (or 3.3 in my case). QT-BUG
                    I will give it a try once qt creator 4.3 is out.
                    Anyway, thanks again for your blog. I hope you keep doing these amazing series.
                    Regards,

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Atr0n
                      wrote on last edited by
                      #19

                      @Schluchti Finally I make it work. The python scripts need to be updated to make it works with python3 since apns2 it's only compatible with it at version 0.52. But everything works like a charm..
                      Thank you a lot.
                      PD: I can pull the python script modification to github if you want.

                      S 1 Reply Last reply
                      1
                      • A Atr0n

                        @Schluchti Finally I make it work. The python scripts need to be updated to make it works with python3 since apns2 it's only compatible with it at version 0.52. But everything works like a charm..
                        Thank you a lot.
                        PD: I can pull the python script modification to github if you want.

                        S Offline
                        S Offline
                        Schluchti
                        wrote on last edited by
                        #20

                        @Atr0n I am glad that it worked :) It would be great if you could create a pull request for it, many thanks!

                        Want to read more about Qt?

                        https://gympulsr.com/blog/qt/

                        Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                        A 1 Reply Last reply
                        1
                        • S Schluchti

                          @Atr0n I am glad that it worked :) It would be great if you could create a pull request for it, many thanks!

                          A Offline
                          A Offline
                          Atr0n
                          wrote on last edited by
                          #21

                          @Schluchti Nice, thanks!
                          I have a final question for you: How do you stop notifications? For example, if the user log out.
                          I was thinking on moving MyGcmListenerService to the main Activity and start it from there. Then I can start and stop the service when I want. What do you think?
                          Regards,

                          S 1 Reply Last reply
                          0
                          • A Atr0n

                            @Schluchti Nice, thanks!
                            I have a final question for you: How do you stop notifications? For example, if the user log out.
                            I was thinking on moving MyGcmListenerService to the main Activity and start it from there. Then I can start and stop the service when I want. What do you think?
                            Regards,

                            S Offline
                            S Offline
                            Schluchti
                            wrote on last edited by Schluchti
                            #22

                            @Atr0n I haven't done that, but I would probably try a different approach first. It's just a personal preference, but moving such a central component to a different place would be to risky for me. (I would be afraid to break something and miss messages in certain occasions).

                            Instead I would try to store the state (user logged in) somewhere and only show the notification when the user is logged in. One could do that either server-side (probably not that comfortable if the user logs in from different devices) or client-side. For example: You could store a boolean flag somewhere (e.q in SharedPreferences, so you can easily access it from the Java part) that saves the state (logged in/logged out). If you receive a new notification you first check if you are logged in. If you aren't, you discard the receive message.

                            But I am not an android expert...so please take that advice with a grain of salt ;-).

                            P.S: The disadvantage of that approach is, that you always receive messages although they aren't displayed. So if you are sending a lot of messages this could produce unnecessary load. Another thing is, that if you have a app, where users can explicitly disable push notifications, you are kind of screwing them over with that approach. (because messages are received anyhow) In that case I would probably go the way you described and look for ways to disable that feature completely (or even better: don't send notifications from the server in the first place)

                            Want to read more about Qt?

                            https://gympulsr.com/blog/qt/

                            Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                            A 1 Reply Last reply
                            1
                            • S Schluchti

                              @Atr0n I haven't done that, but I would probably try a different approach first. It's just a personal preference, but moving such a central component to a different place would be to risky for me. (I would be afraid to break something and miss messages in certain occasions).

                              Instead I would try to store the state (user logged in) somewhere and only show the notification when the user is logged in. One could do that either server-side (probably not that comfortable if the user logs in from different devices) or client-side. For example: You could store a boolean flag somewhere (e.q in SharedPreferences, so you can easily access it from the Java part) that saves the state (logged in/logged out). If you receive a new notification you first check if you are logged in. If you aren't, you discard the receive message.

                              But I am not an android expert...so please take that advice with a grain of salt ;-).

                              P.S: The disadvantage of that approach is, that you always receive messages although they aren't displayed. So if you are sending a lot of messages this could produce unnecessary load. Another thing is, that if you have a app, where users can explicitly disable push notifications, you are kind of screwing them over with that approach. (because messages are received anyhow) In that case I would probably go the way you described and look for ways to disable that feature completely (or even better: don't send notifications from the server in the first place)

                              A Offline
                              A Offline
                              Atr0n
                              wrote on last edited by Atr0n
                              #23

                              @Schluchti Thanks again for your time!
                              Finally, I just want to share with you a piece of code that could help the people follow your tutorials. It's a notification sender using Qt code. I am not a Github guy, so I post it here in case you want to use it (or anyone else). Note that if you are using Windows, you need to install openssl 1.02 in your computer.

                              Edit: This is android only.

                              http.cpp

                              http::http(QObject *parent) : QObject(parent)
                              {
                                  m_manager = new QNetworkAccessManager(this);
                              
                                  QUrl url("https://gcm-http.googleapis.com/gcm/send");
                                  QNetworkRequest request(url);
                                  request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
                                  request.setRawHeader(QByteArray("Authorization"), QByteArray("key=your key here"));
                              
                                  QByteArray data = "{\"data\" : { \"message\": \"Hello world\"}, \"to\" : \"your target here\" }";
                                  connect(m_manager,&QNetworkAccessManager::finished,this,&http::finished);
                                  m_manager->post(request,data);
                                  qDebug() << data;
                              }
                              
                              void http::finished(QNetworkReply *reply)
                              {
                                  qDebug() << reply->error() << reply->errorString();
                                  m_mananger->deleteLater();
                                  m_manager = 0;
                              }
                              

                              http.h

                              #ifndef HTTP_H
                              #define HTTP_H
                              
                              #include <QObject>
                              #include <QNetworkAccessManager>
                              #include <QNetworkReply>
                              
                              class http : public QObject
                              {
                                  Q_OBJECT
                              public:
                                  explicit http(QObject *parent = nullptr);
                              
                              signals:
                              
                              public slots:
                                  void finished(QNetworkReply *reply);
                              
                              private:
                                  QNetworkAccessManager* m_manager;
                              };
                              
                              #endif // HTTP_H
                              
                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved