• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Notification confusion

Kalthasar

Lurker
Nov 3, 2010
3
0
I have a service that is set up to either run in the background (using startForeground(int, Notification) ), or as part of the application that created it.

I have two notifications, and being for different purposes, I want them to do different things.

The first one is just to alert the user that the service is still running in the background, so I've set it's flags as follows:

Code:
notification.flags =  Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR 
                    | Notification.FLAG_ONGOING_EVENT;
The second notification is an error message, which could occur while the service is running in the background, so for now, I've set it's flags as follows:

Code:
notification.defaults = Notification.DEFAULT_ALL
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
Now, as I understand from the API documentation, this second notification should play the default notification sound, vibrate in some default way, and flash the LED in some default pattern.

Instead, all I get is the sound. However, if I add DEFAULT_ALL to the first notification, I get both the sound and vibration as expected, and if I add the FLAG_SHOW_LIGHTS flag to the first one, it will flash the LED as well.

I have also tried defining my own vibration pattern for the second one, as follows:

Code:
long[] vibrate = {0, 100, 200, 100};
notification.vibrate = vibrate;
and only turning on DEFAULT_SOUND and DEFAULT_LIGHTS. Still no vibration.

The only other difference between these two notifications (other than the text and the icon used), is that the first one is displayed using startForeground(int, Notification), and the second is displayed using the notification manager's notify(int, Notification) method.

What am I doing wrong with the second notification?
 
Never mind, I found the problem, I didn't have the vibrate permission set. Mind you, I only discovered this because I got an exception when trying to use the notify() method to display the first notification instead. Why Android treats startForeground() and notifiy() differently in regards to permissions, I do not know.

I do however, have another notification issue that I just discovered. My error message has a pending intent designed to go back to the main activity of my application. The trouble is, this notification can appear while this activity is open. If the user touches the notification message to clear it, it will open a new copy of the activity, and at this point, the user must hit the back button once to return to the original copy of the activity, and then once again to get to the home screen.

Is there a way to ignore the pending intent if the activity in the intent is already open?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones