CrossGeeks/FirebasePushNotificationPlugin

[Android] Condition to display notification

AlleSchonWeg opened this issue · 1 comments

Hi,
the condition, which decides if a notification is shown or not shown confuses me. I talk about this condition:

if ((parameters.TryGetValue(SilentKey, out var silent) && (silent.ToString() == "true" || silent.ToString() == "1")) || (IsInForeground() && (!(!parameters.ContainsKey(ChannelIdKey) && parameters.TryGetValue(PriorityKey, out var imp) && ($"{imp}" == "high" || $"{imp}" == "max")) || (!parameters.ContainsKey(PriorityKey) && !parameters.ContainsKey(ChannelIdKey) && FirebasePushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && FirebasePushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max))))

I init the library without a DefaultNotification channel:
FirebasePushNotificationManager.Initialize(this, false, false);
but i created a custom channel to receive my notifications. For example: "MyChannel".

The first question is: Why the above condition checks the DefaultNotificationChannelImportance? Even if i init wihout DefaultNotification channel.

Next question is about if the app is in foreground. If i send a notification and add the key "channel_id" with value "MyChannel" in the data payload the condition match and no notification is shown. Because of return. Why is the OnReceived Method aborted if app is in foreground and a channel_id is set? If app is in background the message is shown via channel "MyChannel".
Next try i remove the "channel_id" key/value in data payload. Then the OnReceived Method is not canceled. But the channel is set to the default id, which is "FirebasePushNotificationChannel":

var chanId = FirebasePushNotificationManager.DefaultNotificationChannelId;

But i init the library without the Default Channel, so such a channel was not created. The result is a toast message with a warning and no notification is shown.

I cut the condition in pieces:

var c1 = parameters.TryGetValue(SilentKey, out var silent) && (silent.ToString() == "true" || silent.ToString() == "1");
var c2 = !parameters.ContainsKey(ChannelIdKey) && parameters.TryGetValue(PriorityKey, out var imp) && ($"{imp}" == "high" || $"{imp}" == "max");
var c3 = !parameters.ContainsKey(PriorityKey) && !parameters.ContainsKey(ChannelIdKey) && FirebasePushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && FirebasePushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max;

var result = c1|| IsInForeground() && (!c2 || c3);

If i specify ChannelIdKey then c2 is false, because of this: !parameters.ContainsKey(ChannelIdKey). In the last line result becomes true, because c2 is inverted. From false to true. So (true || c3) == true. If IsInForeground() is also true then no notification is shown.
Is this a bug in this condition or why should no notification shown if ChannelIdKey is set and App is in foreground? @rdelrosario Could you explain the idea behind this check? 🤔
Thank you