Skip to content

Commit

Permalink
Ask for notification permission for Android 13+
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasMizera committed Oct 6, 2023
1 parent c835469 commit eb667da
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
28 changes: 28 additions & 0 deletions app/androidutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ QString AndroidUtils::externalStorageAppFolder()
return QString();
}

bool AndroidUtils::requestNotificationPermission()
{
#ifdef ANDROID
double buildVersion = QSysInfo::productVersion().toDouble();

// POST_NOTIFICATIONS permission is available from Android 13+
if ( buildVersion < ANDROID_VERSION_13 )
{
return true;
}

QString notificationPermission = QStringLiteral( "android.permission.POST_NOTIFICATIONS" );

auto r = QtAndroidPrivate::checkPermission( notificationPermission ).result();
if ( r == QtAndroidPrivate::Authorized )
{
return true;
}

r = QtAndroidPrivate::requestPermission( notificationPermission ).result();
if ( r == QtAndroidPrivate::Authorized )
{
return true;
}
#endif
return false;
}

QString AndroidUtils::readExif( const QString &filePath, const QString &tag )
{
#ifdef ANDROID
Expand Down
3 changes: 3 additions & 0 deletions app/androidutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class AndroidUtils: public QObject

static QString externalStorageAppFolder();

// Android 13+ needs permission for sending notifications
static bool requestNotificationPermission();

/**
* Reads EXIF and returns value for given parameters.
* @param filePath Absolute path to a file
Expand Down
11 changes: 9 additions & 2 deletions app/position/tracking/androidtrackingbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "androidtrackingbroadcast.h"
#include "coreutils.h"
#include "inpututils.h"
#include "androidutils.h"

#include <QtCore/private/qandroidextras_p.h>

Expand Down Expand Up @@ -189,8 +190,14 @@ void AndroidTrackingBackend::setupForegroundUpdates()
&AndroidTrackingBackend::sourceUpdatedState
);

// TODO: you need to check if this is starting tracking or just resuming
// It should not be an issue to start this if it is already running, but let's double check
// We need to ask for a permission to show notifications,
// but it is not mandatory to start the foreground service
// See: https://developer.android.com/develop/ui/views/notifications/notification-permission
if ( !AndroidUtils::requestNotificationPermission() )
{
emit errorOccured( tr( "Enable notifications to see tracking in the notifications tray" ) );
CoreUtils::log( QStringLiteral( "Android Tracking Backend" ), QStringLiteral( "Notifications are disabled" ) );
}

auto activity = QJniObject( QNativeInterface::QAndroidApplication::context() );
QAndroidIntent serviceIntent( activity.object(), "uk/co/lutraconsulting/PositionTrackingService" );
Expand Down
1 change: 1 addition & 0 deletions cmake_templates/AndroidManifest.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"/> <!-- Reading compass while taking a picture -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <!-- To be able to send notifications, Android 13+ -->

<!-- Android considers EXIF data as sensitive (since they contain user's location), so we need to opt for permission ACCESS_MEDIA_LOCATION.
Even though it is a runtime permission (one need to opt for it in code),
Expand Down

1 comment on commit eb667da

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 23.10.465211 just submitted!

Please sign in to comment.