Android: Cannot show notification when the App is in foreground
When the notification payload doesn't include priority or channel_id. I expect that the notification will be shown if I set the DefaultNotificationChannelImportance to high or above, but it doesn't.
After reading the source code, I suspect that the first if block in OnReceived of DefaultPushNotificationHandler is wrongly written, as shown below.
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) && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max))))
{
return;
}
For easier reading, I split the condition to multiple parts.
var isSilent = parameters.TryGetValue(SilentKey, out var silent) && (silent.ToString() == "true" || silent.ToString() == "1");
var channelUndefined = !parameters.ContainsKey(ChannelIdKey);
var priorityUndefined = !parameters.ContainsKey(PriorityKey);
var priorityIsOrAboveHigh = parameters.TryGetValue(PriorityKey, out var imp) && ($"{imp}" == "high" || $"{imp}" == "max");
var defaultImportanceBelowHigh = PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max;
if (isSilent || (IsInForeground() && (!(channelUndefined && priorityIsOrAboveHigh) || (priorityUndefined && channelUndefined && defaultImportanceBelowHigh))))
{
return;
}
In my case, the result would be
if (false || (true && (!(true && false) || (true && true && false))))
{
return;
}
and therefore exits the method.
I am not sure is this behaviour a mistake or intended, so I didn't create a pull request for this.
If you want them to be displayed on foreground just set:
PushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.High
In the application class before intializing.
On Fri, Jun 26, 2020, 5:37 AM Paul TO [email protected] wrote:
When the notification payload doesn't include priority or channel_id. I expect that the notification will be shown if I set the DefaultNotificationChannelImportance to high or above, but it doesn't.
After reading the source code, I suspect that the first if block in OnReceived of DefaultPushNotificationHandler is wrongly written, as shown below.
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) && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max)))) { return; }
For easier reading, I split the condition to multiple parts.
var isSilent = parameters.TryGetValue(SilentKey, out var silent) && (silent.ToString() == "true" || silent.ToString() == "1"); var channelUndefined = !parameters.ContainsKey(ChannelIdKey); var priorityUndefined = !parameters.ContainsKey(PriorityKey); var priorityAboveHigh = parameters.TryGetValue(PriorityKey, out var imp) && ($"{imp}" == "high" || $"{imp}" == "max"); var defaultImportanceBelowHigh = PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.High && PushNotificationManager.DefaultNotificationChannelImportance != NotificationImportance.Max;
if (isSilent || (IsInForeground() && (!(channelUndefined && priorityAboveHigh) || (priorityUndefined && channelUndefined && defaultImportanceBelowHigh)))) { return; }
In my case, the result would be
if (false || (true && (!(true && false) || (true && true && false)))) { return; }
and therefore exits the method.
I am not sure is this behaviour a mistake or intended, so I didn't create a pull request for this.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CrossGeeks/PushNotificationPlugin/issues/149, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATOAJYHGNRCGQO66ITSTL3RYRT6PANCNFSM4OJFFF7A .
Below is how I initialize, it is run in OnCreate of my Application class.
PushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.High;
PushNotificationManager.Initialize(this, cats, resetToken, true, false);
The issue is that setting DefaultNotificationChannelImportance to high is not enough, the notification payload must have priority defined and set to high as well.
With notification contains only message and DefaultNotificationChannelImportance set to high:
false || (true && (!(true && false) || (true && true && false))) => true, method exits, notification is NOT made
With notification contains priority high and DefaultNotificationChannelImportance set to high:
false || (true && (!(true && true) || (true && true && false))) => false, method continues, notification is made
@kpto We had the same problem. The Android system creates its own default channel if the channel_id is not included in the push payload.
This behaviour can be suppressed by making an entry in the AndroidManifest.
AndroidManifest.xml
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
MainApplication.cs
//Change for your default notification channel id here.
PushNotificationManager.DefaultNotificationChannelId = Resources.GetString(Resource.String.default_notification_channel_id);