1

I'm developing an Android app, and I'm using GCM to send push notifications. When the user gets a new message, I send a push notification to his phone.

The problem is that when the user reads the message from the webpage, then a useless notification remain on his phone until he cleans it. Is there some sort of method, maybe another push notification, that I can use to remove this useless notification?

I experience something similar in my Gmail app. I receive a notification, and when I read the new mail from my computer the notification just disappears.

How does the Gmail app do this?

Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
stomasso
  • 36
  • 5
  • http://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar – Tyler Oct 02 '13 at 20:12
  • 1
    I think that refer to local notifications. I'm talking about push notifications, I know how to remove notifications from the bar, what I cant figure out is how to do that when the app is not even running. Thanks anyway – stomasso Oct 02 '13 at 20:17
  • that refer to push notifications that were not sent yet. I think anyway, that the answer suit my question – stomasso Oct 02 '13 at 20:40

1 Answers1

0

Edit: I read that wrong in the first place.

Yes, you have to send another GCM message telling your phone to cancel the notification.

minipif
  • 4,756
  • 3
  • 30
  • 39
  • Are you sure that I can call this function through a GCM message? it seems to me that is a method that I can invoke when my app is running. What I want to know, is if there is a way to clear a push notification with the app closed, with another push notification or something similar to notify the system that this notification has no sense now. I achieved such thing developing for iOS, I send an empty-badge0 push notification and all the notification from my app vanishes, so maybe there is an equivalent for android. I'm all eyes. thanks – stomasso Oct 03 '13 at 12:36
  • I'm pretty sure it's safe to call `cancel()` anytime, in the worst case in won't do anything. Your notification being in the notification bar means that your app's process is still alive. I've done so myself and it's working fine. On iOS you don't execute your code when receiving a notification, so they had to put a way to remove notifications, on Android you have to do it yourself. – minipif Oct 03 '13 at 13:48
  • On iOS you don't execute your code when receiving a notification. So in Android you do? Can you execute code when receiving a push notification? Even if you receive it when the app is closed? how? – stomasso Oct 03 '13 at 13:55
  • It executes a function inside your app, in which you write the code to display the notification. I thought you had done this part already (or are you using another way to display the notification that I'm not aware of?). So in your case, you'll have to make a test inside this function to determine weather to display or cancel a notification. – minipif Oct 03 '13 at 14:03
  • We are talking about push notifications, sent from the server. Not local notifications. I didn't write any function to display the notification, none of my code gets executed until the user clicks the notification. – stomasso Oct 03 '13 at 14:07
  • Maybe I'm confused. There is code of my app that gets executed even if the app is closed and the user never clicks the notification? – stomasso Oct 03 '13 at 14:16
  • Just to make it clear, on Android, there are 2 parts needed to display a notification to the user: **First**, your server sends a GCM message to Google, which sends it to your device. But that by itself doesn't do or display anything. **Then**, on your device, inside your app, you execute some code that will display a notification to the user, that he'll eventually click. This is where you'll choose between displaying a new notification or cancelling existing ones. These 2 parts (sending from server and displaying on device) together are needed to mimic iOS' push notification behaviour. – minipif Oct 03 '13 at 14:19
  • I didn't know that, I though that the OS was in charge of process the incoming push notifications and display it in the bar. Thanks for make that clear to me. Maybe titanium automatically implements the onReceive function, so I need to search and maybe modify the libraries. – stomasso Oct 03 '13 at 14:28