Buttons in android push notification - android

I am doing Push notification message in android devices and follows GCM documentation for my reference and I have scenario that in the notification itself I have to show buttons and user clicked it will trigger the respective actions.
From the GCM documentation, in the notification payload we can add click_action to trigger the action when user touched notification...
How to show buttons (Like Accept/Reject ) in the notification message?

You can use .addAction in Notification.Builder.
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_accept, "Accept", prevPendingIntent) // #0
.addAction(R.drawable.ic_reject, "Reject", pausePendingIntent) // #1
// Apply the media style template
EDIT 1
Refer this link.

There is no way to add action buttons to a notification message. This may be a feature added later on but currently it does not exist.
Notification messages allow you to create a very specific type of notification, if you want to have a very custom notification then you should use data messages (not notification messages) and then when the message is received use the Notification.Builder to generate your custom notification with as many features as are available :)

Related

How to detect a change in Notification (Status bar notification) in Android

I'd tried NotificationListenerService provided by Android to listen to notifications by using it's OnNotificationPosted() method. That's all okay. How to detect a change in that notification?
For example: In WhatsApp, if a message is sent and then deleted, It removes the existing notification and sends a new notification as "This message was deleted".
So how to detect that which notification gets changed or updated?
Are you receiving the "changed" notification in onNotificationPosted()? As far as I understand, the notification is replaced instead of changed. So the changes may register as a newly arrived notification.
Update: it turns out "updating" the notification is indeed pushing a new notification but with the same id. So simply track the id of the notification to see which notification was updated.
In your notification listener onNotificationPosted()
public void onNotificationPosted(StatusBarNotification sbn)
{
//this is the notification id
int id = sbn.getId();
...
}

Two notifications with same ID but diffrent Noification channels

I'm generating two local notifications from my app using two different
notification channel. notification configurations will be as below,
1.) Notification1 (notification_channel: msg_1 and notification_id: 1)
2.) Notification2 (notification_channel: msg_2 and notification_id: 1)
What will happen in this case?
It will show single notification
OR
It will show two separate notification
I want to know the expected behaviour from your end?
I've explored about the scenario & it will display as diffrent notification because OS use notificationId to show notification seperately in notificaition stack on tray. If you pass same notificationId ,OS will just overlap older notification with the new one.
Also, notification channel configurations are just applied to notificationId to show specific behaviour.

How to remove an ongoing notification programmatically?

I want to remove the ongoing notification from statusbar.For example whatsapp web notification when whatsapp web is active,usb debugging notification when device is connected to pc which are not removed when swipe.Is it possible or not?If its possible then please help me.Thank u.....
Yes, you can dismiss a notification from statusbar. Just call cancel() for your notification ID.
Check out this link to learn more about removing Notifications: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Removing
If you are hiding only the notification icon on the notification bar (glance view without pulling down the slide bar to see the whole on going notifications), and its notif view still in the on going list, you can pay attention to the method setPriority of NotificationCompat.Builder.
In my case,the codeblock below saved my day!
NotificationCompat.Builder mBuilderOnGoing = new NotificationCompat.Builder(mContext);
if (Build.VERSION.SDK_INT >=16) {
mBuilderOnGoing.setPriority(NotificationCompat.PRIORITY_MIN);
}
Hope it helps

I am getting pushwoosh response null

I have integrated pushwoosh in android application (not in titanium)..
I am successfully getting notification also..
My issue is,
Text is displayed on notification area which I passed from woosh..
BUT NOT ABLE to extract it from intent
intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT)
Also my notification does not remain on notification area..
it only displayed once and disappear, I am not able to click on that
because it is not staying there..as like other notification..!!??
Please can anybody suggest any thing..?
For notification to remain on the notification bar there is falg to set in Android Notification.
Notification not= new Notification(R.drawable.icon,title+" "+context.getString(R.string.Start_in),System.currentTimeMillis());
not.flags=Notification.FLAG_NO_CLEAR;
For Android Notification there is One falg to Clear the notification or not. So I think there will some flag to Not clear the Notification when it arrives.
So Try to find out the flag for not clearing the Notification.
As you are setting flag for sound
pushManager.setSoundNotificationType(SoundType.ALWAYS);

Android: How can I put my notification on top of notification area?

I'm trying to put my notification on top of notification area.
A solution is to set the parameter "when" to my notification object with a future time like:
notification.when = System.currentTimeMills()*2;
The code that I'm using in this:
long timeNotification = System.currentTimeMillis()*2;
Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notification.when = timeNotification;
notification.priority = Notification.PRIORITY_MAX;
but some apps (like Facebook) are able to put a simple notification with their current time over mine.
If I refresh my notification it remains under these ones.
What parameters I have to set to put my Notification to the top of the notifications area?
You should do this. Other answers seem outdated.
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
setPriority(Notification.PRIORITY_MAX) is important. It can also be replaced with any of the following as per requirement.
Different Priority Levels Info:
PRIORITY_MAX --
Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
PRIORITY_HIGH --
Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
PRIORITY_DEFAULT --
Use for all notifications that don't fall into any of the other priorities described here.
PRIORITY_LOW --
Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.
PRIORITY_MIN --
Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.
For more details check the following link:
http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority
You can make your notification Ongoing, when it will appear higher then other usual notification. But in this case user would not be able to clear it manually.
In order to do this set flags to your Notification object:
notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR
Try setting priority of the notification to high
documentation > Notification Priority
Also check this question may it could help you Pin Notification to top of notification area
Please note that if you want a "heads-up" notification i.e., one that displays over the top of the current user window you must have the following set in your builder:
setDefaults(NotificationCompat.DEFAULT_VIBRATE)
The reference is in the javadoc:
A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms.
Complete example for a heads-up notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);

Categories

Resources