-
Notifications
You must be signed in to change notification settings - Fork 75
Add runtime permission request to post notification #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HarelM
wants to merge
2
commits into
capacitor-community:master
Choose a base branch
from
HarelM:post-notification-permission
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ | |
| import android.content.Intent; | ||
| import android.content.IntentFilter; | ||
| import android.content.ServiceConnection; | ||
| import android.content.pm.PackageManager; | ||
| import android.location.Location; | ||
| import android.location.LocationManager; | ||
| import android.net.Uri; | ||
|
|
@@ -45,7 +44,14 @@ | |
| Manifest.permission.ACCESS_FINE_LOCATION | ||
| }, | ||
| alias = "location" | ||
| ), | ||
| @Permission( | ||
| strings = { | ||
| Manifest.permission.POST_NOTIFICATIONS, | ||
| }, | ||
| alias = "notifications" | ||
| ) | ||
|
|
||
| } | ||
| ) | ||
| public class BackgroundGeolocation extends Plugin { | ||
|
|
@@ -76,90 +82,106 @@ public void addWatcher(final PluginCall call) { | |
| call.reject("Service not running."); | ||
| return; | ||
| } | ||
| call.setKeepAlive(true); | ||
| if (getPermissionState("location") != PermissionState.GRANTED && !call.getBoolean("requestPermissions", true)) { | ||
| call.reject("Permission denied.", "NOT_AUTHORIZED"); | ||
| return; | ||
| } | ||
|
|
||
| if (getPermissionState("location") != PermissionState.GRANTED) { | ||
| if (call.getBoolean("requestPermissions", true)) { | ||
| requestPermissionForAlias("location", call, "locationPermissionsCallback"); | ||
| } else { | ||
| call.reject("Permission denied.", "NOT_AUTHORIZED"); | ||
| } | ||
| } else if (!isLocationEnabled(getContext())) { | ||
| call.reject("Location services disabled.", "NOT_AUTHORIZED"); | ||
| if (getPermissionState("location") != PermissionState.GRANTED && call.getBoolean("requestPermissions", true)) { | ||
| call.setKeepAlive(true); | ||
| requestPermissionForAlias("location", call, "locationPermissionsCallback"); | ||
| return; | ||
| } | ||
| if (call.getBoolean("stale", false)) { | ||
| fetchLastLocation(call); | ||
|
|
||
| if (!isLocationEnabled(getContext())) { | ||
| call.reject("Location services disabled.", "NOT_AUTHORIZED"); | ||
| return; | ||
| } | ||
| Notification backgroundNotification = null; | ||
| String backgroundMessage = call.getString("backgroundMessage"); | ||
|
|
||
| if (backgroundMessage != null) { | ||
| Notification.Builder builder = new Notification.Builder(getContext()) | ||
| .setContentTitle( | ||
| call.getString( | ||
| call.setKeepAlive(true); | ||
| addWatcherAfterLocationPermissionGranted(call); | ||
| } | ||
|
|
||
| private Notification createNotification(PluginCall call) { | ||
| String backgroundMessage = call.getString("backgroundMessage"); | ||
| if (backgroundMessage == null) { | ||
| return null; | ||
| } | ||
| Notification.Builder builder = new Notification.Builder(getContext()) | ||
| .setContentTitle( | ||
| call.getString( | ||
| "backgroundTitle", | ||
| "Using your location" | ||
| ) | ||
| ) | ||
| .setContentText(backgroundMessage) | ||
| .setOngoing(true) | ||
| .setPriority(Notification.PRIORITY_HIGH) | ||
| .setWhen(System.currentTimeMillis()); | ||
|
|
||
| try { | ||
| String name = getAppString( | ||
| "capacitor_background_geolocation_notification_icon", | ||
| "mipmap/ic_launcher" | ||
| ); | ||
| String[] parts = name.split("/"); | ||
| // It is actually necessary to set a valid icon for the notification to behave | ||
| // correctly when tapped. If there is no icon specified, tapping it will open the | ||
| // app's settings, rather than bringing the application to the foreground. | ||
| builder.setSmallIcon( | ||
| getAppResourceIdentifier(parts[1], parts[0]) | ||
| ); | ||
| } catch (Exception e) { | ||
| Logger.error("Could not set notification icon", e); | ||
| } | ||
| ) | ||
| ) | ||
| .setContentText(backgroundMessage) | ||
| .setOngoing(true) | ||
| .setPriority(Notification.PRIORITY_HIGH) | ||
| .setWhen(System.currentTimeMillis()); | ||
|
|
||
| try { | ||
| String color = getAppString( | ||
| "capacitor_background_geolocation_notification_color", | ||
| null | ||
| ); | ||
| if (color != null) { | ||
| builder.setColor(Color.parseColor(color)); | ||
| } | ||
| } catch (Exception e) { | ||
| Logger.error("Could not set notification color", e); | ||
| } | ||
| try { | ||
| String name = getAppString( | ||
| "capacitor_background_geolocation_notification_icon", | ||
| "mipmap/ic_launcher" | ||
| ); | ||
| String[] parts = name.split("/"); | ||
| // It is actually necessary to set a valid icon for the notification to behave | ||
| // correctly when tapped. If there is no icon specified, tapping it will open the | ||
| // app's settings, rather than bringing the application to the foreground. | ||
| builder.setSmallIcon( | ||
| getAppResourceIdentifier(parts[1], parts[0]) | ||
| ); | ||
| } catch (Exception e) { | ||
| Logger.error("Could not set notification icon", e); | ||
| } | ||
|
|
||
| Intent launchIntent = getContext().getPackageManager().getLaunchIntentForPackage( | ||
| getContext().getPackageName() | ||
| try { | ||
| String color = getAppString( | ||
| "capacitor_background_geolocation_notification_color", | ||
| null | ||
| ); | ||
| if (launchIntent != null) { | ||
| launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); | ||
| builder.setContentIntent( | ||
| PendingIntent.getActivity( | ||
| getContext(), | ||
| 0, | ||
| launchIntent, | ||
| PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE | ||
| ) | ||
| ); | ||
| if (color != null) { | ||
| builder.setColor(Color.parseColor(color)); | ||
| } | ||
| } catch (Exception e) { | ||
| Logger.error("Could not set notification color", e); | ||
| } | ||
|
|
||
| // Set the Channel ID for Android O. | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| builder.setChannelId(BackgroundGeolocationService.class.getPackage().getName()); | ||
| } | ||
| Intent launchIntent = getContext().getPackageManager().getLaunchIntentForPackage( | ||
| getContext().getPackageName() | ||
| ); | ||
| if (launchIntent != null) { | ||
| launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); | ||
| builder.setContentIntent( | ||
| PendingIntent.getActivity( | ||
| getContext(), | ||
| 0, | ||
| launchIntent, | ||
| PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| backgroundNotification = builder.build(); | ||
| // Set the Channel ID for Android O. | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| builder.setChannelId(BackgroundGeolocationService.class.getPackage().getName()); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private void addWatcherAfterLocationPermissionGranted(PluginCall call) { | ||
| if (call.getBoolean("stale", false)) { | ||
| fetchLastLocation(call); | ||
| } | ||
| if (call.getString("backgroundMessage") != null && getPermissionState("notification") != PermissionState.GRANTED) { | ||
| requestPermissionForAlias("notifications", call, "notificationsPermissionsCallback"); | ||
| return; | ||
| } | ||
| service.addWatcher( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be replaced with a direct call to |
||
| call.getCallbackId(), | ||
| backgroundNotification, | ||
| call.getFloat("distanceFilter", 0f) | ||
| call.getCallbackId(), | ||
| createNotification(call), | ||
| call.getFloat("distanceFilter", 0f) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -170,15 +192,22 @@ private void locationPermissionsCallback(PluginCall call) { | |
| call.reject("User denied location permission", "NOT_AUTHORIZED"); | ||
| return; | ||
| } | ||
| if (call.getBoolean("stale", false)) { | ||
| fetchLastLocation(call); | ||
| } | ||
| if (service != null) { | ||
| service.onPermissionsGranted(); | ||
| // The handleOnResume method will now be called, and we don't need it to call | ||
| // service.onPermissionsGranted again so we reset this flag. | ||
| stoppedWithoutPermissions = false; | ||
| } | ||
| addWatcherAfterLocationPermissionGranted(call); | ||
| } | ||
|
|
||
| @PermissionCallback | ||
| private void notificationsPermissionsCallback(PluginCall call) { | ||
| service.addWatcher( | ||
| call.getCallbackId(), | ||
| createNotification(call), | ||
| call.getFloat("distanceFilter", 0f) | ||
| ); | ||
| } | ||
|
|
||
| @PluginMethod() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these nested
ifblocks are easier to understand. Why did you change it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a hard time understanding the flow... I guess it's a matter of taste...
I also saw that keepAlive was called in a place it didn't need to be called due to the nesting of the ifs...