Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26253] Android: NotificationManager.notify() will crash while screen is off and if app is missing WAKE_LOCK permission

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2018-09-12T00:52:46.000+0000
Affected Version/sRelease 6.2.0
Fix Version/sRelease 7.5.0
ComponentsAndroid
Labelsandroid, notification, permissions
ReporterJoshua Quick
AssigneeJoshua Quick
Created2018-08-03T00:45:55.000+0000
Updated2018-09-21T21:19:02.000+0000

Description

*Summary:* As of Titanium 6.2.0, posting a local notification while the screen is off will cause the app to crash if the app does not have the WAKE_LOCK permission. Titanium should not require the WAKE_LOCK permission. *Steps to reproduce:*

Connect an Android device to your machine.

Turn the Android device's screen off.

Build and run the below code on that Android device.

View the log and notice an exception was printed on startup.

Turn on the device's screen and view the app. Notice an exception dialog was displayed.

Ti.Android.NotificationManager.notify(123, Ti.Android.createNotification({
	contentTitle: "Titanium Notification",
	contentText: "Content Text",
	contentIntent: Ti.Android.createPendingIntent({
		intent: Ti.App.Android.launchIntent,
	}),
}));
*Result:* The following exception gets logged...
[ERROR] :  TiExceptionHandler: Error: Neither user 10256 nor current process has android.permission.WAKE_LOCK.
[ERROR] :  TiExceptionHandler:     at /ActivitySingleTaskTest.js:29:32
[ERROR] :  TiExceptionHandler:     at Module._runScript (ti:/module.js:613:9)
[ERROR] :  TiExceptionHandler:     at Module.load (ti:/module.js:105:7)
[ERROR] :  TiExceptionHandler:     at Module.loadJavascriptText (ti:/module.js:457:9)
[ERROR] :  TiExceptionHandler:     at Module.loadAsFile (ti:/module.js:512:15)
[ERROR] :  TiExceptionHandler:     at Module.loadAsFileOrDirectory (ti:/module.js:429:20)
[ERROR] :  TiExceptionHandler:     at Module.require (ti:/module.js:296:17)
[ERROR] :  TiExceptionHandler:     at require (ti:/module.js:570:15)
[ERROR] :  TiExceptionHandler:     at /app.js:7:1
[ERROR] :  TiExceptionHandler:     at Module._runScript (ti:/module.js:596:17)
[ERROR] :  TiExceptionHandler:
[ERROR] :  TiExceptionHandler:     android.os.Parcel.readException(Parcel.java:1942)
[ERROR] :  TiExceptionHandler:     android.os.Parcel.readException(Parcel.java:1888)
[ERROR] :  TiExceptionHandler:     android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:405)
[ERROR] :  TiExceptionHandler:     android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:1342)
[ERROR] :  TiExceptionHandler:     android.os.PowerManager$WakeLock.acquire(PowerManager.java:1326)
[ERROR] :  TiExceptionHandler:     ti.modules.titanium.android.notificationmanager.NotificationManagerModule.notify(NotificationManagerModule.java:159)
[ERROR] :  TiExceptionHandler:     org.appcelerator.kroll.runtime.v8.V8Runtime.nativeRunModule(Native Method)
[ERROR] :  TiExceptionHandler:     org.appcelerator.kroll.runtime.v8.V8Runtime.doRunModule(V8Runtime.java:184)
[ERROR] :  TiExceptionHandler:     org.appcelerator.kroll.KrollRuntime.runModule(KrollRuntime.java:247)
[ERROR] :  TiExceptionHandler:     org.appcelerator.titanium.TiLaunchActivity.loadActivityScript(TiLaunchActivity.java:135)
[
*Expected Result:* App should not require the WAKE_LOCK permission. Java code should check if app has WAKE_LOCK before attempting to use it. If the permission is missing, then avoid it and do not turn on the screen. Note that many Android users tend to avoid apps that use the WAKE_LOCK permission since they're considered to waste battery life. This permission is a dirty word in the Android world and is best avoided if possible. *Cause:* This is an issue in Titanium's Java NotificationManagerModule.notify() method... [NotificationManagerModule.java#L137](https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/android/src/java/ti/modules/titanium/android/notificationmanager/NotificationManagerModule.java#L137) The solution is to do the following: * Check if WAKE_LOCK permission is defined in "AndroidManifest.xml" via the Context class before creating a wake lock. * catch block should catch the Exception type to catch all exception.

Comments

  1. Joshua Quick 2018-09-07

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/10311
  2. Lokesh Choudhary 2018-09-12

    FR Passed. Waiting for merge to be enabled.
  3. Lokesh Choudhary 2018-09-12

    PR Merged.
  4. Keerthi Mahalingam 2018-09-21

    Verified the fix on Sdk 7.5.0.v20180921103412. Works fine. Closing.
       Operating System
         Name                        = Mac OS X
         Version                     = 10.13.6
         Architecture                = 64bit
       Node.js
         Node.js Version             = 8.9.1
         npm Version                 = 5.5.1
       Titanium CLI
         CLI Version                 = 5.1.1
       Titanium SDK
         SDK Version                 = 7.5.0.v20180921103412
       Device                            =Samsung s5 android 6, Pixel android 8
       

JSON Source