Unsolved GCM push notifications android
-
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. -
@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.
-
@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 usingAnt
. 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 usingAnt
. 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