Android Question Notification question

Sonny

Member
Licensed User
Longtime User
Hello all.

Really simple problem:

I want to use the notification area JUST for the purpose of displaying messages to the user. I DO NOT want to launch any activities when the user taps on the notification.

I've been looking around at all of the "Advanced Notification Libraries" and they all appear to have mandatory activity launches associated with their use.

B4A's core notification object does NOT support this action. I would assume that by passing "" or NULL for the activity would serve this purpose - but no - doing so makes the library relaunch the existing activity. Kind of confusing, since the Android documentation states that activity launch IS optional, so I would assume that that functionality would have been available in the core notification object.

Thanks in advance.

> Sonny <
 

mangojack

Expert
Licensed User
Longtime User
Sonny , from what I gather you just don't include a module name in the Notification.SetInfo method .. ie

B4X:
Sub Process_Globals
  
   Dim sNotif As Notification  

End Sub

Sub Service_Create
  sNotif.Initialize
  sNotif.Icon = "icon"
 
'the last parameter in the next line is the module to be started when the user presses on Notification
sNotif.SetInfo("My Service Info", "This is a message to the user ....","")

  sNotif.Sound = False
  sNotif.Notify(1)  
  Service.StartForeground(1,sNotif)
  
End Sub
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
Sonny , from what I gather you just don't include a module name in the Notification.SetInfo method .. ie

B4X:
Sub Process_Globals
 
   Dim sNotif As Notification 

End Sub

Sub Service_Create
  sNotif.Initialize
  sNotif.Icon = "icon"

'the last parameter in the next line is the module to be started when the user presses on Notification
sNotif.SetInfo("My Service Info", "This is a message to the user ....","")

  sNotif.Sound = False
  sNotif.Notify(1) 
  Service.StartForeground(1,sNotif)
 
End Sub


Thanks for the reply, MangoJack.

But did you REALLY read my request? Leaving the parameter blank automatically relaunches the existing activity - that's the problem I have with the function - it doesn't follow common logic.

You and I both agree that leaving the Activity name out should mean that no Activity launching would happen - but it's coded in a way to force us to launch an Activity - even if we don't want it to. That's the problem I have with it, and it's clearly stated in the B4A documentation that that is the way it works:

"Pass an empty string to start the current activity (when calling from an activity module)."

So how do I tell it then NOT to launch ANY activity at all - if leaving it blank means RELAUNCH the existing activity?

Yeah, it beats the hell out of me too why it would be coded this way.

> Sonny <
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I want to use the notification area JUST for the purpose of displaying messages to the user. I DO NOT want to launch any activities when the user taps on the notification.

Sonny .. Yes I read your post.

The code I posted is from a current service that runs to monitor phone calls.
B4X:
  sNotif.SetInfo("HangUp & POQ", "Monitoring for Telemaketing Calls ....",Main)
when I press on my notification it restarts the Main activity .. which has be closed down.
by replacing the last parameter 'Main' with an empty string it does NOT launch my 'Main' activity ...

I'm not a services guru .. maybe i'm missing something
 
Last edited:
Upvote 0

Sonny

Member
Licensed User
Longtime User
Sonny .. Yes I read your post.

The code I posted is from a current service that runs to monitor phone calls.
B4X:
  sNotif.SetInfo("HangUp & POQ", "Monitoring for Telemaketing Calls ....",Main)
when I press on my notification it restarts the Main activity .. which has be closed down.
by replacing the last parameter 'Main' with an empty string it does NOT launch my 'Main' activity ...

I'm not a services guru .. maybe i'm missing something

Dude, you are correct in that it does not launch main - it relaunches whatever activity you have running.

An example - this is not the problem - just meant to just illustrate the action.

Let's say, that from main, you launch an activity called "test". In "test", through your "Activity_Resume" sub, you read values from a file to init some variables. Change the values of those variables, fire off your notification. When you tap on the notification - the "Activity_Resume" sub will relaunch - and your variables will be reinitialized by the file read again, because your "test" activity is being relaunched.

I'm not running this as a service. I'm just firing off a notification from my running activity, and when the notification is acknowledged by the user, the running activity is "rerun" from the start - clearing my state back to initial start. This is happening because the notification call will ALWAYS fire off some activity - whether I want it to or not. This is a flaw in the implementation of the notification feature.

Look closely at the B4A autocompletion popup for "n.SetInfo" under "Description". It clearly states the action - which is what I find to be undesirable - and is what I'm looking to find a work around for, without having to write a wrapper for the Android library myself.

I don't want this action to "undo" whatever state I'm in by restarting the activity again.

Do you see my point now?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I understand now , my inexperience led me to think notifications were only run from services.
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
Erel,

Since no one is presenting any solutions, would you please shed some light on this?

The Android SDK provides the option of NOT launching an activity with a notification. B4A's notification object does not. How can I work around this?

Thanks, in advance.
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
An intent is required on Android 2.3-. If you are not targeting these devices then it is possible to create a notification without an activity using reflection library (I can show you the required code).

http://stackoverflow.com/questions/...intent-required-in-notificationcompat-builder


Erel,

Yes, please show me the required code. I'm interested in targeting ICS 4.0.4 and above - also providing me with an effective way of dealing with 2.3.5, etc. would be greatly appreciated.

Thanks for the assist.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
also providing me with an effective way of dealing with 2.3.5, etc. would be greatly appreciated.
You cannot use the same notification on Android 2.3.x if you remove the activity. A simple solution that will work with all devices is to create a "dummy" activity and call Activity.Finish from Activity_Create.
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
Erel,

Well, the problem I have is that the activity I'm running does a lot of computations, and then draws a screen based on those operations. If I call a "dummy activity", then returning to my "real activity" will cause it to re-run all of the calculations and repaint the resulting graphics again. I'm trying to avoid doing the all of this a second time. De-coupling my logic - and dispersing it into a second activity - doesn't seem to improve the situation much either.

The most direct route would appear to be an access to the SDK's notification call through reflection. Being able to do this for ICS and above is good enough for my purposes. Could you please provide some B4A code to do this?

Thanks.
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
Whether I use "Activity_Resume" or "Activity_Create" - the problem remains the same - when I return from your suggested "empty activity", my calling activity will execute BOTH "Activity_Resume" and "Activity_Create" subs needlessly a second time. This is what I need to avoid happening.
 
Upvote 0
Top