GCM push notifications android



  • Hello, I am developing an android application and I would like to implement GCM push notifications. Any suggestions how to do it?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Currently Qt doesn't have anything specific that so you would need to write some native code for that. However you might be interested by V-Play and it's GCM plugin.


  • Qt Champions 2016

    @SGaist I can imagine that there are some devs out there already done this work. I'm really missing a central page at Qt where all such native integrations with Android or iOS can be mentioned. would be better to do work only one time and would make it easier for new devs using Qt for mobile. @tero-kojo we already discussed this some time ago. any news ?



  • I am currently working on a blog series about typical problems that developers are facing when using Qt for mobile app development. I think that might be a good topic for the next blog post. :)


  • Lifetime Qt Champion

    @ekkescorner sur there is, but usually specific to their own applications except in the case of V-Play that provides the support through their framework.

    @Schluchti In the categories of stuff that can help:


  • Qt Champions 2016

    @SGaist V-Play is not a solution for me - I'm using Qt because it's Open Source and I want to use Open Source for any plugins / extensions / libraries. V-Play is a great SDK, but V-Play PlugIns are not OSS

    Mobile Apps are special. Developing mobile Apps with Qt there are many common things missed: PushServices, Access to Contacts, Phone, Android Intents, iOS App Extensions, Battery state, SignaturePad, ... all those stuff you usually need, but not found from Qt Classes.
    And you need it for Android and iOS and probably W10. All mobile devs together could develop a community market place. Not for Qt in general - for Qt mobile Apps. Current solution is that devs have to learn HowTo create the native Android / iOS stuff and HowTo call this from Qt. I'm sure there are many devs doing the same again and again.

    Would be cool to have a common place where devs can provide solutions and take a look at other devs solutions HowTo handle such kind of stuff for Android and iOS.

    take a look at PhoneGab / Cordova Plugins: http://www.plugreg.com/plugins and http://www.plugreg.com/plugins or per ex. a SignaturePad for ReactNative: https://github.com/RepairShopr/react-native-signature-capture I found from https://react.parts/native. Try to find a Signature Pad for QQC2 apps running on Android and iOS.
    there was one Qt Blog about Intents with Qt for Android, part 1 and nothing else. also no info HowTo solve similar use-case für iOS.
    There are also some great blogs from KDAB.
    Now with QQC2 we have the UI Controls we need for mobile, but I'm really missing a central place from Qt from where you can find all those important extensions/libraries for mobile app developers.

    ... seems I'm running out of scope from this thread ;-)



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


  • Qt Champions 2016

    @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



  • Hi,

    As already mentioned by @SGaist, V-Play provides support for Push Notifications via GCM and OneSignal for Android and iOS. Also, local notifications are possible with V-Play.

    It's really convenient to add, check out this code sample for OneSignal:

    import QtQuick 2.1
    import VPlayPlugins 1.0
    
     OneSignal {
       id: onesignal
    
       appId: "<ONESIGNAL-APP-ID>"
    
       onNotificationReceived: {
         console.debug("Received notification with message:", message)
       }
     }
    

    That's all the code you'll need!

    As pointed out by @ekkescorner, you will need a V-Play Indie- or Enterprise license starting from $49 / month to use these features.

    By the way, support for Firebase is currently in the pipeline ;)

    Cheers,
    Lorenz



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



  • 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!



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    @Schluchti cool - blog look great :)

    probably next 2 weeks I'll test it.

    just still optimizing all my apps for Qt 5.8



  • This post is deleted!


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



  • @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,



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



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



  • @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,



  • @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)



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

Log in to reply