Question: When my app is in foreground i am able to see the notification in notification tray too. How can I remove it and how to handle push payload automatically?
Answer:
This is a default behavior for our SDK. If you want to override it and handle notifications in a specific way, you need to create a custom Notification Service Extension and override OnMessageReceived method. In order not to show a notification in tray, you should just return true with OnMessageReceived. Please find a short code snippet below:
public class NotificationServiceExtension : Pushwoosh.Notification.NotificationServiceExtension
{
public static Activity CurrentActivity { get; set; }
protected override bool OnMessageReceived(PushMessage message)
{
if (IsAppOnForeground)
{
Handler mainHandler = new Handler(ApplicationContext.MainLooper);
mainHandler.Post(() =>
{
HandlePush(message);
});
return true; // true indicates that a notification should not be shown
}
return false;
}
protected override void StartActivityForPushMessage(PushMessage message)
{
if (!HandlePush(message))
{
base.StartActivityForPushMessage(message);
}
}
[MainThread]
bool HandlePush(PushMessage message)
{
// your custom foreground push handling here
}
}
If you want to perform additional actions (i.e. handle push payload) when receiving a push, you should also do it in your OnMessageReceived (HandlePush function in example above).
You can find a detailed sample in our Github repo:
https://github.com/Pushwoosh/pushwoosh-xamarin/blob/master/Samples/Droid/PushwooshSample/NotificationServiceExtension.cs
Comments
0 comments
Please sign in to leave a comment.