diff options
author | Ryan Chu <ryan.chu@qt.io> | 2018-04-11 16:48:38 +0200 |
---|---|---|
committer | Ryan Chu <ryan.chu@qt.io> | 2018-06-07 06:32:05 +0000 |
commit | d870b6d262a2f22ca7793c6336717bd5101e8fc7 (patch) | |
tree | 3a63b6654c9f33d4b91c09728ff0f800c5f006a3 | |
parent | d683d8b556f924a67aaa0012c417f9346b46ec1a (diff) |
Example of Android Push Notification with Firebase Console
This example demonstrates receiving push notifications from the Firebase
Console, and then displays the content on the screen in JSON format. You
can send push notifications to the devices running this example by using
the notifications composer in the Firebase Console. The Firebase servers
will notify the registered application that the new message is available
to sync.
Task-number: QTPM-182
Change-Id: Ie84ce7c1446af3a6bcf7db4be9911db2b18f08ef
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Ari Salmi <snowgrains@snowgrains.com>
18 files changed, 642 insertions, 6 deletions
diff --git a/examples/examples.pro b/examples/examples.pro new file mode 100644 index 0000000..c39edda --- /dev/null +++ b/examples/examples.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +# Example of Firebase (Cloud Messaging) Push Notification +firebase: SUBDIRS += firebase diff --git a/examples/firebase/firebase.pro b/examples/firebase/firebase.pro new file mode 100644 index 0000000..295cbe1 --- /dev/null +++ b/examples/firebase/firebase.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS += pushnotification diff --git a/examples/firebase/pushnotification/android-sources/AndroidManifest.xml b/examples/firebase/pushnotification/android-sources/AndroidManifest.xml new file mode 100644 index 0000000..33f4b6d --- /dev/null +++ b/examples/firebase/pushnotification/android-sources/AndroidManifest.xml @@ -0,0 +1,72 @@ +<?xml version="1.0"?> +<manifest package="org.qtproject.qt5.android.pushnotification" + xmlns:android="http://schemas.android.com/apk/res/android" + android:versionName="1.0" android:versionCode="1" android:installLocation="auto"> + <application android:icon="@drawable/icon" + android:name="org.qtproject.qt5.android.bindings.QtApplication" + android:label="@string/app_name"> + <activity android:configChanges="orientation|uiMode|screenLayout|screenSize + |smallestScreenSize|locale|fontScale|keyboard + |keyboardHidden|navigation" + android:name="org.qtproject.qt5.android.pushnotification.NotificationActivity" + android:label="Qt PushNotification" + android:screenOrientation="unspecified" + android:launchMode="singleTop"> + <intent-filter> + <action android:name="android.intent.action.MAIN"/> + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + <meta-data android:name="android.app.lib_name" + android:value="-- %%INSERT_APP_LIB_NAME%% --"/> + <meta-data android:name="android.app.qt_sources_resource_id" + android:resource="@array/qt_sources"/> + <meta-data android:name="android.app.repository" android:value="default"/> + <meta-data android:name="android.app.qt_libs_resource_id" + android:resource="@array/qt_libs"/> + <meta-data android:name="android.app.bundled_libs_resource_id" + android:resource="@array/bundled_libs"/> + <!-- Deploy Qt libs as part of package --> + <meta-data android:name="android.app.bundle_local_qt_libs" + android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/> + <meta-data android:name="android.app.bundled_in_lib_resource_id" + android:resource="@array/bundled_in_lib"/> + <meta-data android:name="android.app.bundled_in_assets_resource_id" + android:resource="@array/bundled_in_assets"/> + <!-- Run with local libs --> + <meta-data android:name="android.app.use_local_qt_libs" + android:value="-- %%USE_LOCAL_QT_LIBS%% --"/> + <meta-data android:name="android.app.libs_prefix" + android:value="/data/local/tmp/qt/"/> + <meta-data android:name="android.app.load_local_libs" + android:value="-- %%INSERT_LOCAL_LIBS%% --"/> + <meta-data android:name="android.app.load_local_jars" + android:value="-- %%INSERT_LOCAL_JARS%% --"/> + <meta-data android:name="android.app.static_init_classes" + android:value="-- %%INSERT_INIT_CLASSES%% --"/> + <!-- Messages maps --> + <meta-data android:value="@string/ministro_not_found_msg" + android:name="android.app.ministro_not_found_msg"/> + <meta-data android:value="@string/ministro_needed_msg" + android:name="android.app.ministro_needed_msg"/> + <meta-data android:value="@string/fatal_error_msg" + android:name="android.app.fatal_error_msg"/> + <!-- Messages maps --> + <!-- Splash screen --> + <!-- + <meta-data android:name="android.app.splash_screen_drawable" + android:resource="@drawable/logo"/> + --> + <!-- Splash screen --> + </activity> + <service android:name="com.google.firebase.messaging.MessageForwardingService" + android:exported="false"> + </service> + </application> + <uses-sdk android:minSdkVersion="16" /> + <supports-screens android:largeScreens="true" android:normalScreens="true" + android:anyDensity="true" android:smallScreens="true"/> + <!-- %%INSERT_PERMISSIONS --> + <uses-permission android:name="android.permission.INTERNET"/> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> + <!-- %%INSERT_FEATURES --> +</manifest> diff --git a/examples/firebase/pushnotification/android-sources/build.gradle b/examples/firebase/pushnotification/android-sources/build.gradle new file mode 100644 index 0000000..1399764 --- /dev/null +++ b/examples/firebase/pushnotification/android-sources/build.gradle @@ -0,0 +1,69 @@ +buildscript { + repositories { + google() + jcenter() + } + + dependencies { + classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.google.gms:google-services:3.2.0' + } +} + +repositories { + google() + jcenter() + maven { + url "https://maven.google.com" + } + flatDir { + dirs "$System.env.GOOGLE_FIREBASE_SDK" + "/libs/android" + } +} + +apply plugin: 'com.android.application' + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.google.android.gms:play-services-base:12.0.1' + compile 'com.google.firebase:firebase-messaging:12.0.1' + compile 'com.google.firebase.messaging.cpp:firebase_messaging_cpp@aar' +} + +apply plugin: 'com.google.gms.google-services' + +android { + /******************************************************* + * The following variables: + * - androidBuildToolsVersion, + * - androidCompileSdkVersion + * - qt5AndroidDir - holds the path to qt android files + * needed to build any Qt application + * on Android. + * + * are defined in gradle.properties file. This file is + * updated by QtCreator and androiddeployqt tools. + * Changing them manually might break the compilation! + *******************************************************/ + + compileSdkVersion androidCompileSdkVersion.toInteger() + + buildToolsVersion androidBuildToolsVersion + + sourceSets { + main { + manifest.srcFile 'AndroidManifest.xml' + java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java'] + aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl'] + res.srcDirs = [qt5AndroidDir + '/res', 'res'] + resources.srcDirs = ['src'] + renderscript.srcDirs = ['src'] + assets.srcDirs = ['assets'] + jniLibs.srcDirs = ['libs'] + } + } + + lintOptions { + abortOnError false + } +} diff --git a/examples/firebase/pushnotification/android-sources/google-services.json b/examples/firebase/pushnotification/android-sources/google-services.json new file mode 100644 index 0000000..2267b54 --- /dev/null +++ b/examples/firebase/pushnotification/android-sources/google-services.json @@ -0,0 +1,2 @@ +# YOU SHOULD REPLACE THIS WITH YOUR OWN GOOGLE-SERVICES.JSON FILE! +# See https://firebase.google.com/docs/cpp/setup diff --git a/examples/firebase/pushnotification/android-sources/res/drawable/icon.png b/examples/firebase/pushnotification/android-sources/res/drawable/icon.png Binary files differnew file mode 100644 index 0000000..035c600 --- /dev/null +++ b/examples/firebase/pushnotification/android-sources/res/drawable/icon.png diff --git a/examples/firebase/pushnotification/android-sources/src/org/qtproject/example/pushnotification/NotificationActivity.java b/examples/firebase/pushnotification/android-sources/src/org/qtproject/example/pushnotification/NotificationActivity.java new file mode 100644 index 0000000..f39b116 --- /dev/null +++ b/examples/firebase/pushnotification/android-sources/src/org/qtproject/example/pushnotification/NotificationActivity.java @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +package org.qtproject.qt5.android.pushnotification; + +import android.content.Intent; +import com.google.firebase.messaging.MessageForwardingService; + +public class NotificationActivity extends org.qtproject.qt5.android.bindings.QtActivity +{ + @Override + protected void onNewIntent(Intent intent) + { + Intent message = new Intent(this, MessageForwardingService.class); + message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT); + message.putExtras(intent); + message.setData(intent.getData()); + startService(message); + } +} diff --git a/examples/firebase/pushnotification/doc/images/pushnotification-example.png b/examples/firebase/pushnotification/doc/images/pushnotification-example.png Binary files differnew file mode 100644 index 0000000..6b60dd7 --- /dev/null +++ b/examples/firebase/pushnotification/doc/images/pushnotification-example.png diff --git a/examples/firebase/pushnotification/doc/src/qtcloudmessaging-firebase-pushnotification.qdoc b/examples/firebase/pushnotification/doc/src/qtcloudmessaging-firebase-pushnotification.qdoc new file mode 100644 index 0000000..9206964 --- /dev/null +++ b/examples/firebase/pushnotification/doc/src/qtcloudmessaging-firebase-pushnotification.qdoc @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example firebase/pushnotification + \title Firebase Push Notification Example + \ingroup examples-qtcloudmessaging + \brief Demonstrates Firebase Cloud Messaging support with Qt. + + \image pushnotification-example.png Screenshot of the example + + \e {Firebase Cloud Messaging (FCM)} is a messaging solution. It reliably + delivers messages via the \e FCM servers. Using \e FCM, you can send + notification messages to notify a client app that a new message is available + to sync. To learn more, visit \l + {https://firebase.google.com/docs/cloud-messaging}. + + This example demonstrates receiving push notifications from the \e {Firebase + Console}, and then displays the content on the screen in JSON format. You + can send push notifications to devices running this example by using the + notifications composer in the \e {Firebase Console}. The fields such as \e + message-text and \e custom-data are sent to this application in a payload + composed of key/value pairs. + + To use this example, the user must install \e {Firebase SDK} to their + environment. See https://firebase.google.com/docs/android/setup + \note Set environment variable \e GOOGLE_FIREBASE_SDK to the root directory + of \e {Firebase C++ SDK}. + \note Download the registered \c google-service.json from the \e {Firebase + Console} and save it as \c android-sources/google-services.json, replacing + the sample file. + + \include examples-run.qdocinc +*/ diff --git a/examples/firebase/pushnotification/main.cpp b/examples/firebase/pushnotification/main.cpp new file mode 100644 index 0000000..5eaf25d --- /dev/null +++ b/examples/firebase/pushnotification/main.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtQuick> + +#include "pushnotification.h" + +int main(int argc, char **argv) +{ + QGuiApplication app(argc, argv); + QQuickView view; + + PushNotification notifications(QStringLiteral("GoogleFireBase"), QStringLiteral("MobileClient"), + &view); + + QQmlContext *ctxt = view.rootContext(); + ctxt->setContextProperty(QStringLiteral("notifications"), notifications.getService()); + + view.setResizeMode(QQuickView::SizeRootObjectToView); + view.setSource(QUrl(QStringLiteral("qrc:/qml/main.qml"))); + view.show(); + + if (!notifications.startService()) { + if (QObject *message = view.rootObject()->findChild<QObject*>("firebase_message")) + message->setProperty("text", "Starting service of PushNotification fails!"); + } + + return app.exec(); +} diff --git a/examples/firebase/pushnotification/main.qrc b/examples/firebase/pushnotification/main.qrc new file mode 100644 index 0000000..69145a8 --- /dev/null +++ b/examples/firebase/pushnotification/main.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>qml/main.qml</file> + </qresource> +</RCC> diff --git a/examples/firebase/pushnotification/pushnotification.cpp b/examples/firebase/pushnotification/pushnotification.cpp new file mode 100644 index 0000000..1d62049 --- /dev/null +++ b/examples/firebase/pushnotification/pushnotification.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "pushnotification.h" + +PushNotification::PushNotification(const QString &provider, const QString &client, QObject *parent) + : QObject(parent), providerId(provider), clientId(client) {} + +PushNotification::~PushNotification() +{ + // Disconnect the clients from the registered provider. + for (const auto &clientId : cloudMessage.localClients(providerId)) + cloudMessage.disconnectClient(providerId, clientId); + + // Deregister the service provider and remove the provider id. + // It will return immediately if the providerId is unregistered. + cloudMessage.deregisterProvider(providerId); +} + +bool PushNotification::startService() +{ + // A SERVER_API_KEY that authorizes your app for access to Google services, + // including sending topic messages via the Firebase Cloud Messaging + // protocols. You can get it in the Cloud Messaging tab of the Firebase + // console settings. + QVariantMap params; + params[QStringLiteral("SERVER_API_KEY")] = "Get your API key from the Firebase console"; + + // Registering the Google firebase service component. + if (cloudMessage.registerProvider(providerId, &firebaseProvider, params)) + return !cloudMessage.connectClient(providerId, clientId).isEmpty(); + + return false; +} diff --git a/examples/firebase/pushnotification/pushnotification.h b/examples/firebase/pushnotification/pushnotification.h new file mode 100644 index 0000000..ca04862 --- /dev/null +++ b/examples/firebase/pushnotification/pushnotification.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PUSHNOTIFICATION_H +#define PUSHNOTIFICATION_H + +#include <QObject> +#include <QCloudMessaging> +#include <QtCloudMessagingFirebase> + +class PushNotification: public QObject +{ + Q_OBJECT +public: + PushNotification(const QString &provider, const QString &client, QObject *parent = nullptr); + ~PushNotification(); + + bool startService(); + QCloudMessaging *getService() {return &cloudMessage;} + +private: + Q_DISABLE_COPY(PushNotification) + + const QString providerId; + const QString clientId; + + QCloudMessaging cloudMessage; + QCloudMessagingFirebaseProvider firebaseProvider; +}; + +#endif // PUSHNOTIFICATION_H diff --git a/examples/firebase/pushnotification/pushnotification.pro b/examples/firebase/pushnotification/pushnotification.pro new file mode 100644 index 0000000..d30611e --- /dev/null +++ b/examples/firebase/pushnotification/pushnotification.pro @@ -0,0 +1,27 @@ +QT += quick cloudmessagingfirebase + +ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources + +SOURCES += \ + main.cpp \ + pushnotification.cpp + +OTHER_FILES += \ + qml/main.qml \ + android-sources/src/org/qtproject/example/pushnotification/NotificationActivity.java \ + android-sources/AndroidManifest.xml \ + android-sources/build.gradle \ + android-sources/google-services.json \ + android-sources/res/drawable/icon.png + +RESOURCES += \ + main.qrc + +HEADERS += \ + pushnotification.h + +GOOGLE_FIREBASE_SDK = $$(GOOGLE_FIREBASE_SDK) +!isEmpty(GOOGLE_FIREBASE_SDK): INCLUDEPATH += $${GOOGLE_FIREBASE_SDK}/include + +target.path = $$[QT_INSTALL_EXAMPLES]/firebase/pushnotification +INSTALLS += target diff --git a/examples/firebase/pushnotification/qml/main.qml b/examples/firebase/pushnotification/qml/main.qml new file mode 100644 index 0000000..2e85e32 --- /dev/null +++ b/examples/firebase/pushnotification/qml/main.qml @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCloudMessaging module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Rectangle { + width: 500 + height: 500 + color: "white" + + Column { + anchors.fill: parent + Text { + id: title + color: "black" + font.pixelSize: parent.width / 20 + text: "Qt Push Notification" + width: parent.width + horizontalAlignment: Text.AlignHCenter + } + Text { + id: firebase_message + objectName: "firebase_message" + color: "black" + text: "Waiting for the message from Firebase" + wrapMode: Text.WrapAtWordBoundaryOrAnywhere + width: parent.width + } + } + Connections { + target: notifications + onMessageReceived: { + console.log(message) + firebase_message.text = message + } + } +} diff --git a/src/cloudmessaging/qcloudmessagingrestapi.cpp b/src/cloudmessaging/qcloudmessagingrestapi.cpp index c59e538..07fdb73 100644 --- a/src/cloudmessaging/qcloudmessagingrestapi.cpp +++ b/src/cloudmessaging/qcloudmessagingrestapi.cpp @@ -67,13 +67,15 @@ QCloudMessagingRestApi::QCloudMessagingRestApi(QObject *parent) : QObject(parent), d(new QCloudMessagingRestApiPrivate) { - - d->m_online_state = d->m_network_info.isOnline(); d->m_wait_for_last_request_response = false; d->m_waiting_counter = 0; +#ifndef QT_NO_BEARERMANAGEMENT + d->m_online_state = d->m_network_info.isOnline(); + connect(&(d->m_network_info), &QNetworkConfigurationManager::onlineStateChanged, this, &QCloudMessagingRestApi::onlineStateChanged); +#endif connect(&(d->m_manager), &QNetworkAccessManager::authenticationRequired, this, &QCloudMessagingRestApi::provideAuthentication); diff --git a/src/cloudmessaging/qcloudmessagingrestapi_p.h b/src/cloudmessaging/qcloudmessagingrestapi_p.h index 71e0547..7f13561 100644 --- a/src/cloudmessaging/qcloudmessagingrestapi_p.h +++ b/src/cloudmessaging/qcloudmessagingrestapi_p.h @@ -43,7 +43,10 @@ #include <QNetworkReply> #include <QTimer> #include <QNetworkAccessManager> + +#ifndef QT_NO_BEARERMANAGEMENT #include <QNetworkConfigurationManager> +#endif QT_BEGIN_NAMESPACE @@ -74,7 +77,9 @@ public: QTimer m_msgTimer; bool m_online_state; QList <QCloudMessagingNetworkMessage> m_network_requests; +#ifndef QT_NO_BEARERMANAGEMENT QNetworkConfigurationManager m_network_info; +#endif int m_waiting_counter; int m_server_message_timer; int m_server_wait_for_response_counter; diff --git a/src/cloudmessagingfirebase/cloudmessagingfirebase.pro b/src/cloudmessagingfirebase/cloudmessagingfirebase.pro index e9ad425..fe7fc5e 100644 --- a/src/cloudmessagingfirebase/cloudmessagingfirebase.pro +++ b/src/cloudmessagingfirebase/cloudmessagingfirebase.pro @@ -30,10 +30,13 @@ INCLUDEPATH += $$(GOOGLE_FIREBASE_SDK)/include android { QT += androidextras - - LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/armeabi-v7a/gnustl/libmessaging.a - LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/armeabi-v7a/gnustl/libapp.a - + equals(ANDROID_TARGET_ARCH, x86) { + LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/x86/gnustl/libmessaging.a + LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/x86/gnustl/libapp.a + } else { + LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/armeabi-v7a/gnustl/libmessaging.a + LIBS += $$(GOOGLE_FIREBASE_SDK)/libs/android/armeabi-v7a/gnustl/libapp.a + } } else: macos { LIBS += -F$$(GOOGLE_FIREBASE_SDK)/frameworks/darwin \ -framework firebase \ |