CrossGeeks / FirebasePushNotificationPlugin

Firebase Push Notification Plugin for Xamarin iOS and Android
MIT License
396 stars 178 forks source link

Get all notifications when app starts #289

Open kerberosargos opened 4 years ago

kerberosargos commented 4 years ago

Hello, firstly thank you for sharing your great plugin with us.

Simple question; is there any way to access all notification when OnNotificationOpened event raise?

For example; I am sending multiple notifications when app is background. When I tap a notification from android notification bar I can read only current notification data which I have tapped. I would like to read all notification in stack and clear all from android notification bar. is it possible or not?

Thank you in advance.

rdelrosario commented 4 years ago

At this moment we don't support it but sounds like an interesting feature. Will consider adding it on future versions.

kerberosargos commented 4 years ago

Thank you for your answer. I would like to cache notifications on the device. I am newbie in Xamarin. Any suggestion to access local android notifications stack in this time?

kerberosargos commented 4 years ago

@rdelrosario Hello again. I have wrote a Dependency Injection Service for Android. May be it can be useful for you. But I can not get custom param which it comes from payload.

using Android.App;
using MyApp.Droid.Customizers;
using MyApp.Services;
using System;
using System.Collections.Generic;

[assembly: Xamarin.Forms.Dependency(typeof(AndroidNotificationManagerDependencyService))]
namespace MyApp.Droid.Customizers
{

    public class AndroidNotificationManagerDependencyService : IAndroidNotificationManagerDependencyService
    {
        public Dictionary<string, Dictionary<string, string>> GetNotifications()
        {

            var result = new Dictionary<string, Dictionary<string, string>>();

            NotificationManager notificationManager = Application.Context.GetSystemService(Android.Content.Context.NotificationService) as NotificationManager;

            var statusBarNotification = notificationManager?.GetActiveNotifications();

            if (statusBarNotification.Length > 0)
            {

                foreach (var notify in statusBarNotification)
                {

                    string id = notify.Id.ToString();

                    if (!result.ContainsKey(id))
                    {
                        var messageDetails = new Dictionary<string, string> {
                            {"id", id },
                            {"tag", notify.Tag.ToString() },
                            {"message", notify.Notification.Extras.Get("android.title").ToString() },
                            {"body", notify.Notification.Extras.Get("android.text").ToString() }
                        };

                        result.Add(id, messageDetails);

                        foreach (var key in notify.Notification.Extras.KeySet())
                        {
                            Console.WriteLine("Notification.Extras.Key: " + key);
                        }

                    }
                }
            }

            return result;

        }
    }
}

Sample payload

{
    "to" : "cxiXJ5BT6jY:APA91bFA1eWR9lPuoAcodao4i8CjgyBtaFSI2yhqZpIr1HoGGCgon6IPPJAQuNW4BF_AIsf7Mtg6k2Ba9JCgkkRxJfcIW1vkjoGgbmDTVzRjc7voWj_jiyWx6STscoOM6rd8Cso2TKDk",
    "priority": "high",
    "data": {
        "title" : "sample title",
        "custom_key" : "sample custom key",
        "body":"sample body",
        "id" : "1",
        "tag" : "tagfor1"
    },
    "notification": {
        "content_available" : true
    }
}

I would like to get custom_key param. Any idea? Thank you in advance.