Skip to content
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

Android 10 updates #1891

Merged
merged 24 commits into from
Mar 19, 2023
Merged

Android 10 updates #1891

merged 24 commits into from
Mar 19, 2023

Conversation

timkellypa
Copy link
Contributor

Android 10 support.

  • Android 10 device support.
  • Android 10 SDK support (androidx libraries).

Note: This request is dependant on my other PR's (which are included). These include changes for Android 8+ Notification Channel support, autoLaunch support, and support to adjust volume controls in Android when a notification is fired.

timkellypa and others added 8 commits October 2, 2018 17:12
Created ability to have multiple channels
generated dynamically for vibrate/sound configurations.
* removed extra changes caused by creating a new branch *
- Added alarmVolume and resetDelay parameters.
These let the caller specify the volume how long to stay at that
volume before resetting.

- Added requestDoNotDisturbPermissions and hasDoNotDisturbPermissions
These allow the caller to check and request permissions to
take the user out of silent (for an alarm volume config).  This
also enables vibration when the phone is on silent (otherwise
vibration and volume do not work on Android 8+).
Auto launches app and triggers an event.
- Update wakelock to acquire a partial lock for a second and not release.
This allows it to run a small amount of async code in the trigger event.

- Add methods to disable battery saving mode.
This allows autolaunch alarms to fire more reliably.

- Add channelId parameter, to allow caller to override channelId.
(Was formerly always generated)
- Updates to support Android 10 (device and SDK)
- Change to androidx imports
- Conditionally skip autoLaunch behavior on Android 10
- Some bug fixes with channel naming and firing notifications.

Firing Notifications if autoLaunch:
- No longer fires notifications if autoLaunch is true.
- In above case, trigger event should schedule an immediate notification
- The intent is that it now can be conditional
- Still fires notification immediately on Android 10 when app is closed
Pull changes from original repo (katzer).
- Typo prevented custom files from playing.
- This ensures timely firing when using an autoLaunch receiver.
- Add longer timeout to wakelock.
- Make wakelock timeout configurable (wakeLockTimeout)
- Fix max priority notification on <M. Was not passing a wakeup flag.
- Added ability to use fullScreenIntent.
- Fixed some things that were causing triggers not to fire.
- Added ability to configure in-app triggers, decoupled from autoLaunch.
- Default to immediately firing notifications
   - in instances where inapp triggers won't be reliably executed
   - based on Android version, sleep mode, etc.
- Should not be called at all on < Oreo.
- Wasn't introduced until API 20, so threw errors on earlier versions.
- Fix iOS schedule method to remove existing notification first.
- Refactor Android receivers.
- Create a base receiver with the logic for triggering a notification.
- Extend that receiver for both restore and trigger.
- Use same notification logic on boot as you do during a normal trigger.
- Remove clear.  No reason to clear a notification on restart.
- Remove duplicate rescheduling logic.
- If alarm is triggered on restore, performNotification reschedules it.
@expresscalendar
Copy link

Hi, I've been playing around with code in this and other beta branches but cannot get it to work on my OnePlus with Oxygen9.
The issue is with triggering notifications in the future - the notifications is correctly scheduled (and AlarmManager triggered at correct time ass shown in logcat) but the reciever does not get called (the onRecieve breakpoint is not hit in Android Studio).
I was wondering what OSes have you tested this with?
Thanks.

@timkellypa
Copy link
Contributor Author

Hi, I've been playing around with code in this and other beta branches but cannot get it to work on my OnePlus with Oxygen9.
The issue is with triggering notifications in the future - the notifications is correctly scheduled (and AlarmManager triggered at correct time ass shown in logcat) but the reciever does not get called (the onRecieve breakpoint is not hit in Android Studio).
I was wondering what OSes have you tested this with?
Thanks.

@expresscalendar I've really only tested my changes with Samsung S9, S10, and a few older Samsung devices. There may be something specific to that OS that is not making it fire. I'm also testing with a very specific use case. My updates are just so my particular application will work, and I've not really done regression testing on features that I do not use.

However, my application does rely on exact notification times, so I may be able to help, assuming your problem is not OS-specific.

A few things to try if you rely on exact, real-time notifications, and if you happen to be testing this while the device is idle:

  1. Be sure you set the priority to the max, 2. That makes it use setAlarmClock instead of setExactAndAllowWhileIdle, the latter of which does not actually guarantee that it will fire on time.
  2. Set wakeup to true. That will make it acquire a wakelock.
  3. You may need to remove the app from battery optimization. I've added a couple methods to the plugin to manage this (requestIgnoreBatteryOptimizations being the main one) and have documented how they work, but beware that I've heard of that causing issues with the app store unless you have a good reason.

If you have an example of the object that you're sending to the notification plugin's schedule method, I can give it a try on one of my devices to see if I can reproduce your issue. Then at least we'll know if it's specific to that device/OS.

@expresscalendar
Copy link

@timkellypa Thanks for getting back to me so promptly.
The issue is the app is wide awake and the problem persists. I went back and added extra priority flag alas to no avail. Here is the full notification object as returned from getScheduled() call:

{"id":1589449628661,
"title":"Event title",
"text":"Starts ‎Today at 09:18‎.",
"trigger":{"at":1589447880000,"type":"calendar"},
"priority":2,
"wakeup":true,
"data":"{"instanceId":"5ebd0c9a98ae411750dbfd3d","scheduleId":"5e94a25356625650f4c2f3af","updatedOn":1589447834694}",
"smallIcon":"res://ic_launcher.png",
"color":"#56D8FF",
"launch":true,
"actions":[],
"alarmVolume":-1,
"attachments:[],
"autoLaunch":false,
"autoClear":true,
"clock":true,
"defaults":0,
"groupSummary":false,
"led":true,
"lockscreen":true,
"number":0,
"progressBar":{"enabled":false,"value":0,"maxValue":100,"indeterminate":false},
"resetDelay":5,
"silent":false,
"sound":true,
"timeoutAfter":null,
"vibrate":false,
"fullScreenIntent":false,
"triggerInApp":false,
"meta":{"plugin":"cordova-plugin-local-notification","version":"0.9-beta.3"}}

Strangely the meta field shows version 0.9-beta.3? I do have the correct package installed in node-modules (shows beta-4).

Bizzarely I have managed to get it working for one notification (triggered in the future) at a time using #1862 (comment) - another notification added before the first one was triggered resulted in 'cancelation' of the first one.

Anyway, I'll try to reset my device and start afresh as adb shell dumpsys alarm reveals thousands of pending alarms for my app.

@expresscalendar
Copy link

@timkellypa Right, after device restart with no scheduled alarms the problem persists. I get a
05-14 11:02:00.008 2547 3313 V AlarmManager: Triggering alarm #1: 0 when =1589450520000 package =com.expresscalendar.app operation =*walarm*:NOTIFICATION_ID314132658-2 flags =0x3 entry in the log showing the alarm was correctly run but the notification does not get shown.

I'll go back to #1862 (comment) to see what the diff in code is that made the notification work before.

@timkellypa
Copy link
Contributor Author

timkellypa commented May 14, 2020

@timkellypa Right, after device restart with no scheduled alarms the problem persists. I get a
05-14 11:02:00.008 2547 3313 V AlarmManager: Triggering alarm #1: 0 when =1589450520000 package =com.expresscalendar.app operation =*walarm*:NOTIFICATION_ID314132658-2 flags =0x3 entry in the log showing the alarm was correctly run but the notification does not get shown.

I'll go back to #1862 (comment) to see what the diff in code is that made the notification work before.

@expresscalendar I think in my app, I always have an "after" on the trigger. I'm not looking at the code now, but when you go to schedule, maybe set

trigger: { after: new Date().getTime() }

I think that parameter was relatively new to new versions of the plugin and is used as a fail safe to make sure alarms don't fire for things that were scheduled in the past. It couldn't really be defaulted in the builder because the notifications are rebuilt prior to firing.

As far as the version being wrong, that was my fault. I didn't update it in all the proper locations. I don't know if/when it will be merged and what the version will be then, but just in case, I'll try to fix that up when I get a chance.

@expresscalendar
Copy link

expresscalendar commented May 14, 2020

Thanks @timkellypa, so I have discovered the culprit in why #1862 (comment) schedules only a single notification at a time - I used timestamp for my notification id which is > java.Integer.MAX and it got truncated which resulted in all my notifs having the same bloody id. Argh. Anyway, got it working with THAT branch, now I'm going to test with yours.

@timkellypa
Copy link
Contributor Author

@expresscalendar sounds good, thanks! Let me know if there are some additional changes I need to pull in to get it working for you. I'd be happy to do so.

@snow-drop snow-drop mentioned this pull request May 30, 2020
Zoe and others added 3 commits June 16, 2020 16:14
@expresscalendar
Copy link

@timkellypa, apologies for getting back to you soo late but had my hands tied with crazy deadlines. I can now confirm your branch works for OnePlus, after the manifest target fix.
Thanks a lot for good work!

@timkellypa
Copy link
Contributor Author

@expresscalendar glad things are working for you! Thanks for helping to track the issue down. Please let me know if you run into any other issues.

@amit-lulla
Copy link

@timkellypa : When I try to use your code, I get an error on Android build. Please guide what is it that I'm missing in my code?

(truncated) ... error: package NotificationCompat does not exist
NotificationCompat.Builder builder;
error: cannot find symbol
Message[] messages = options.getMessages();
^
symbol: class Message
location: class Builder
error: package NotificationCompat does not exist
NotificationCompat.MessagingStyle style;
error: package NotificationCompat does not exist
style = new NotificationCompat.MessagingStyle("Me")

de/appplant/cordova/plugin/notification/Builder.java:253:

- When app is in background, system may not let it do async tasks.
- Disable in app triggers when app is not in foreground.
- This prevents inconsistent behavior with in app triggers
@timkellypa
Copy link
Contributor Author

@timkellypa : When I try to use your code, I get an error on Android build. Please guide what is it that I'm missing in my code?

(truncated) ... error: package NotificationCompat does not exist
NotificationCompat.Builder builder;
error: cannot find symbol
Message[] messages = options.getMessages();
^
symbol: class Message
location: class Builder
error: package NotificationCompat does not exist
NotificationCompat.MessagingStyle style;
error: package NotificationCompat does not exist
style = new NotificationCompat.MessagingStyle("Me")

de/appplant/cordova/plugin/notification/Builder.java:253:

@amit-lulla It seems that the references to the androidx libraries are broken for you. If you are not targeting Android 10 in your project, please see if the following note in my changelog helps you.

"Note: If you are not building with API 29 on Android, you can use https://www.npmjs.com/package/cordova-plugin-androidx for backwards compatibility."

If not let me know and I'll see if there's something else going on.

@shankari
Copy link

In case anybody else is looking to see when this will be merged, I switched to using the version from https://github.com/timkellypa/cordova-plugin-local-notifications.git instead and it works fine for me.

cordova plugin rm cordova-plugin-local-notifications
cordova plugin add https://github.com/timkellypa/cordova-plugin-local-notifications.git

- Handle multiple notifications firing simultaneously.
- Refactor volume manager to new class.
- FLAG_UPDATE_CURRENT is better for clearing out old notifications.
- FLAG_CANCEL_CURRENT can sometimes keep stale alarms around.
- Store base date for repeating notifications that are rescheduled.
- On reboot, do not fire high priority notifications that already fired.
@RafaelKr
Copy link

RafaelKr commented Sep 8, 2020

Thanks @shankari! But there's a little mistake in the remove command, it should be "notification" instead of "notifications"

cordova plugin rm cordova-plugin-local-notification
cordova plugin add https://github.com/timkellypa/cordova-plugin-local-notifications.git

@mstoecke
Copy link

@timkellypa Any chances to change background and text color of notifications?

@alixti
Copy link

alixti commented Oct 28, 2020

I had some troubles building @timkellypa fork (I had this one using AndroidX and others with incompatible libraries). But I fixed by using this plugin created by @dpa99c

Next, you need to enable the AndroidX support by adding the next code into your project's config.xml file:

<preference name="AndroidXEnabled" value="true" />

Good luck and hope this helps!

- Dispatch a visibility change event on the webview.
- Allows running of asynchronous code in webview (otherwise not working)
- Remove no longer used checkAppInForeground() methods in receivers
@jbagaresgaray
Copy link

Any news on this PR? When is this going to be merge or approved?

@timkellypa
Copy link
Contributor Author

timkellypa commented Jul 27, 2021

@timkellypa Any chances to change background and text color of notifications?

@mstoecke It doesn't look like it currently supports this, sorry. I haven't done extensive updates to this plugin beyond ones I've needed for a specific use case, but am unopposed to accepting a PR for this if it doesn't break existing functionality.

You can change the color of the title text of the notification by using "color," but the background might be trickier, since it seems to require applying a style to the notification as well.

@Guy-Sela
Copy link

I had some troubles building @timkellypa fork (I had this one using AndroidX and others with incompatible libraries). But I fixed by using this plugin created by @dpa99c

Next, you need to enable the AndroidX support by adding the next code into your project's config.xml file:

<preference name="AndroidXEnabled" value="true" />

Good luck and hope this helps!

@alixti @timkellypa

Thanks.

Works on android though, but not on ios, so I'm using the master version for ios, and this setup for android.

Is that intentional? Known? A bug? Or am I missing something?

@gazben
Copy link

gazben commented May 2, 2022

Any news on this PR?

@GitToTheHub GitToTheHub merged commit ba42c73 into katzer:master Mar 19, 2023
ParhamTaher added a commit to HitGrab/cordova-plugin-local-notifications that referenced this pull request Oct 27, 2023
commit eb0ac58
Merge: 97cffd5 7d21d76
Author: Manuel Beck <manuelbeck87@outlook.de>
Date:   Sun Mar 19 18:21:06 2023 +0100

    Merge pull request katzer#1973 from kumarsunil0007/patch-1

    Updated version for appShortcutBadger extenstion from 1.1.19 to 1.1.22

commit 97cffd5
Merge: ba42c73 e56ad35
Author: Manuel Beck <manuelbeck87@outlook.de>
Date:   Sun Mar 19 17:51:07 2023 +0100

    Merge pull request katzer#1979 from ygpedroso/bug/use-gradle-implementation

    Migrate to Gradle implementation

commit ba42c73
Merge: 6b30ca8 3c8e928
Author: Manuel Beck <manuelbeck87@outlook.de>
Date:   Sun Mar 19 17:46:13 2023 +0100

    Merge pull request katzer#1891 from timkellypa/android-10-updates

    Android 10 updates

commit 6b30ca8
Author: Manuel Beck <manuelbeck87@outlook.de>
Date:   Fri Mar 3 08:40:45 2023 +0100

    Update README.md

    Removed fund raising

commit d50a574
Author: Manuel Beck <manuelbeck87@outlook.de>
Date:   Tue Feb 28 17:33:19 2023 +0100

    Update README.md

    Added fundraising campaign for updating the plugin.

commit e56ad35
Author: Yannier <yannier@rideco.com>
Date:   Mon Aug 8 20:00:56 2022 -0400

    Migrate to Gradle implementation

    - Starting on Gradle 7.0 the compile method is removed.
    - Trying to build an app making use of the compile method returns
    the following: > Could not find method compile() for arguments...
    - This change migrates compile instances in the Gradle config file
    to use implementation instead

commit 7d21d76
Author: Sunil Kumar <67047295+kumarsunil0007@users.noreply.github.com>
Date:   Tue May 3 11:18:41 2022 +0530

    Update localnotification.gradle

    Updated version for appShortcutBadger from 1.1.19 to 1.1.22

commit 3c8e928
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Fri Jan 29 19:16:43 2021 -0500

    Fix "launchInApp" notifications in background

    - Dispatch a visibility change event on the webview.
    - Allows running of asynchronous code in webview (otherwise not working)
    - Remove no longer used checkAppInForeground() methods in receivers

commit 965d356
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Tue Aug 4 21:43:25 2020 -0400

    Persist base date within trigger.

    - Store base date for repeating notifications that are rescheduled.
    - On reboot, do not fire high priority notifications that already fired.

commit 989f924
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Thu Jul 30 00:21:52 2020 -0400

    Fix flag for scheduled alarm pending intent.

    - FLAG_UPDATE_CURRENT is better for clearing out old notifications.
    - FLAG_CANCEL_CURRENT can sometimes keep stale alarms around.

commit 613e0d8
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Sun Jul 26 23:57:17 2020 -0400

    Fix volume configuration concurrency.

    - Handle multiple notifications firing simultaneously.
    - Refactor volume manager to new class.

commit f561c33
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Mon Jul 13 18:19:34 2020 -0400

    Fix Android 8+ alarms when app is in background

    - When app is in background, system may not let it do async tasks.
    - Disable in app triggers when app is not in foreground.
    - This prevents inconsistent behavior with in app triggers

commit 74a8028
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Tue Jun 16 22:19:18 2020 -0400

    Fix version to beta.4 in local-notification.js

commit 9689811
Merge: bda9b6e 32caf60
Author: timkellypa <timkellypa@gmail.com>
Date:   Tue Jun 16 22:10:46 2020 -0400

    Merge pull request #1 from kassamina/master

    AndroidManifest.xml can end up missing the config for local notifications

commit 32caf60
Author: Zoe <zoe@tovifit.com>
Date:   Tue Jun 16 16:14:25 2020 -0400

    Add m0dch3n's fix for targeting the manifest

commit bda9b6e
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Thu May 7 00:26:39 2020 -0400

    Minor fixes to restore receiver.

    - Remove clear.  No reason to clear a notification on restart.
    - Remove duplicate rescheduling logic.
    - If alarm is triggered on restore, performNotification reschedules it.

commit 802963c
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Wed May 6 00:44:01 2020 -0400

    iOS bug fix.  Android restore receiver enhancement

    - Fix iOS schedule method to remove existing notification first.
    - Refactor Android receivers.
    - Create a base receiver with the logic for triggering a notification.
    - Extend that receiver for both restore and trigger.
    - Use same notification logic on boot as you do during a normal trigger.

commit 52002b0
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Wed Apr 29 15:26:26 2020 -0400

    Fix isInteractive() call.

    - Should not be called at all on < Oreo.
    - Wasn't introduced until API 20, so threw errors on earlier versions.

commit 4472efe
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Wed Apr 29 12:22:51 2020 -0400

    Bug fixes and added parameters

    - Added ability to use fullScreenIntent.
    - Fixed some things that were causing triggers not to fire.
    - Added ability to configure in-app triggers, decoupled from autoLaunch.
    - Default to immediately firing notifications
       - in instances where inapp triggers won't be reliably executed
       - based on Android version, sleep mode, etc.

commit 7721ecb
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Thu Apr 16 01:50:12 2020 -0400

    Use AlarmClock for more exact high priority alarms

commit 3b63bf0
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Mon Apr 13 23:29:23 2020 -0400

    Wakelock fixes.

    - Add longer timeout to wakelock.
    - Make wakelock timeout configurable (wakeLockTimeout)
    - Fix max priority notification on <M. Was not passing a wakeup flag.

commit 43f788b
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Fri Apr 10 22:24:25 2020 -0400

    Use a more aggressive wakelock

    - This ensures timely firing when using an autoLaunch receiver.

commit 9aa59c1
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Mon Apr 6 19:29:06 2020 -0400

    Fix typo in AssetUtil provider string

    - Typo prevented custom files from playing.

commit ef8cb32
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Thu Apr 2 23:53:33 2020 -0400

    Add changelog for this branch.

commit 2b0a6b4
Merge: 876fb35 caff55e
Author: Tim Kelly <tim.kelly@ert.com>
Date:   Thu Apr 2 10:23:56 2020 -0400

    Merge branch 'master' into android-10-updates

    Pull changes from original repo (katzer).

commit 876fb35
Author: Tim Kelly <tim.kelly@ert.com>
Date:   Thu Apr 2 10:11:38 2020 -0400

    Android 10 updates

    - Updates to support Android 10 (device and SDK)
    - Change to androidx imports
    - Conditionally skip autoLaunch behavior on Android 10
    - Some bug fixes with channel naming and firing notifications.

    Firing Notifications if autoLaunch:
    - No longer fires notifications if autoLaunch is true.
    - In above case, trigger event should schedule an immediate notification
    - The intent is that it now can be conditional
    - Still fires notification immediately on Android 10 when app is closed

commit 211973d
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Fri Oct 4 02:02:48 2019 -0400

    Change order in permissions section of readme.

commit 27a0bac
Author: Tim Kelly <timkellypa@gmail.com>
Date:   Fri Oct 4 01:51:17 2019 -0400

    Updates for wakelock, battery saving, and channels.

    - Update wakelock to acquire a partial lock for a second and not release.
    This allows it to run a small amount of async code in the trigger event.

    - Add methods to disable battery saving mode.
    This allows autolaunch alarms to fire more reliably.

    - Add channelId parameter, to allow caller to override channelId.
    (Was formerly always generated)

commit 10c6cdb
Author: timkellypa <timkellypa@gmail.com>
Date:   Thu Nov 1 22:46:14 2018 -0400

    Adding auto-launch capability for notifications in Android.
    Auto launches app and triggers an event.

commit 02832e9
Author: timkellypa <timkellypa@gmail.com>
Date:   Fri Oct 5 12:39:45 2018 -0400

    Added ability to customize alarm volume.
    - Added alarmVolume and resetDelay parameters.
    These let the caller specify the volume how long to stay at that
    volume before resetting.

    - Added requestDoNotDisturbPermissions and hasDoNotDisturbPermissions
    These allow the caller to check and request permissions to
    take the user out of silent (for an alarm volume config).  This
    also enables vibration when the phone is on silent (otherwise
    vibration and volume do not work on Android 8+).

commit 9c61255
Author: timkellypa <timkellypa@gmail.com>
Date:   Tue Oct 2 14:31:44 2018 -0400

    Added Android 8 Support for Sound and Vibrate properties.
    Created ability to have multiple channels
    generated dynamically for vibrate/sound configurations.
    * removed extra changes caused by creating a new branch *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.