I'm using Google Cloud Messaging to receive new orders into an app. I'm trying to handle cases where the same order is sent twice. I just want the second receipt to be ignored, unfortuntately when the app is in the background I dont seem to be able to cancel the notification (ie it still makes a noise and sends a message). The app works fine when in the foreground, putting cancel notification code in my GCMBrodacastreceiver doesnt seem to do anything. Am I missing something?
NotificationManager mNotify = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotify.cancelAll();
You can set the "tag" field in the notification payload. If you use this the incoming notification will just update any existing one with the same tag.
cancelAll() will dismiss the notification, but your code may not be invoked when the app is in the background as the notification will be posted without your app's code running. One option would be to have your server not send the notification if it has already sent one recently.
Related
I'm using Firebase Cloud Messaging for Android. When my app is in the foreground, FCM will invoke onMessageReceived on my app's FirebaseMessagingService subclass.
When my app is in the background, the Android OS will create a default notification entry in the system tray. That notification entry looks pretty good to me; for the notifications I need to send, I don't particularly need to interrupt the user with the notification. The default notification in the system tray is just fine.
My question is, how do I make that "default" notification happen in onMessageReceived when my app is in the foreground? Is there a way to say, "I don't need to intercept this notification; please just do what you'd normally do if I were in the background"?
(Do I have to simulate it by hand with NotificationCompat.Builder? If so, which settings do I need to pass to get default behavior?)
When the app is in the background, your notifications are processed by the Google Services, which takes care of displaying your notifications as required, including the default click action (opening the app) and the notification icon.
When the app is in the foreground, the received messages are processed by the app.
Yes, you will have to mimic the NotificationCompat.Builder to look like default. There is no other way to do this without intercepting with onMessageReceived() callback.
Reference: https://firebase.google.com/docs/cloud-messaging/android/receive
Is it possible/is there a reliable way to get a call back when a notification is received on the android platform? i.e. I want to record when the notification is received (not when it is later opened).
No, It's your app which receives the message so if you need things logged, then log it. Simply handle it as you like once you receive it. Note that if you use FCM with Notification message message type, then you will not be directly notified about message arrival when app is in background (notification will be automatically posted by Firebase - your app will get message payload once use taps the notification). In such case consider switching to Data messages so you will get it no matter your app is in foreground or not.
See docs: https://firebase.google.com/docs/cloud-messaging/downstream
My little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .
I've been used urban airship for notification and it's working.
Now client's requirement has been, he want to update a specific data (ie: current location) for client once any silent notification(no status bar notification) received. Could any one help me on this. Thanks
There is a couple of ways to send a silent notification to android devices. If you are using the built in notification builders, the notification will return null when the alert is empty. If you are using a custom one, just make sure its returning null when the alert is empty.
Then, send a notification with an empty alert. It will still come through on the push broadcast receiver.
I need to have a toggle for push notifications in my app, but I can't make changes in server. Can I intercept the push message in GCM and not show it in the topbar?
Since you are the one displaying the notifications, you do not have to "intercept" anything. You are already receiving the GCM messages, with the code that you wrote, where your code is displaying the Notification. Simply have that code examine your SharedPreferences (or wherever the "toggle" is stored) to see if the code should actually display the Notification.