FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

[Android] Condition to display notification

Open AlleSchonWeg opened this issue 2 years ago • 1 comments

Hi, the condition, which decides if a notification is shown or not shown confuses me. I talk about this condition: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/d86266a9f45687b418f5f1e69c348681d1ff6e27/Plugin.FirebasePushNotification/DefaultPushNotificationHandler.android.cs#L151

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": https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/d86266a9f45687b418f5f1e69c348681d1ff6e27/Plugin.FirebasePushNotification/DefaultPushNotificationHandler.android.cs#L167 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.

AlleSchonWeg avatar Jul 25 '22 09:07 AlleSchonWeg

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

AlleSchonWeg avatar Jul 26 '22 14:07 AlleSchonWeg