Closed eliaspuurunen closed 5 years ago
Is the web portal showing successes or failures for the push notifications that aren't being received? You should see a success rate in the detail view of the push notification in the UI.
@achocron yes - the notifications appear on the device as well.
To clarify - the notifications are being displayed on the device (iPad) and can be tapped, but after the 1st time the event fires, the event from the AppCenter Push SDK will no longer fire.
Got it. I can't seem to reproduce - on my end, the event is fired each time I tap a new notification. Are you subscribing to the event before or after you start App Center?
@achocron before I start AppCenter. Should we try after/should we only subscribe after?
protected override void OnStart()
{
if (!AppCenter.Configured)
{
Push.PushNotificationReceived += OnGotPush;
}
//AppCenter.LogLevel = LogLevel.Verbose;
// Actual keys omitted for privacy
AppCenter.Start("android=00000000-0000-0000-0000-000000000000;" +
"uwp=00000000-0000-0000-0000-000000000000;" +
$"ios={iOSKey}",
typeof(Push));
this.MainPage = new NavigationPage(new LoginPage());
}
@achocron Additional information: we are also using a plugin to show local notifications.
https://github.com/EgorBo/Toasts.Forms.Plugin/tree/master/Toasts.Forms.Plugin.iOS
This morning I removed any initialization calls to the plugin from our code. AppCenter Push is now working exactly as expected. It appears there is a conflict between this plugin and AppCenter Push.
Question: if the app is in the foreground, how should we handle display of notifications? Is there a suggested plugin to use for Xamarin Forms that will not break AppCenter.Push?
In the following file, it appears that once the plugin is called to display a local notification, the notification center delegate is replaced with the plugin's implementation.
// Line 20 of Toasts.Forms.Plugin/Toasts.Forms.Plugin.iOS/UNNotificationManager.cs
var notificationCenter = UNUserNotificationCenter.Current;
// Code omitted for brevity
notificationCenter.Delegate = new UserNotificationCenterDelegate(id, (identifier, notificationResult) =>
{
lock (_lock)
if (_resetEvents?.ContainsKey(identifier) == true && _eventResult?.ContainsKey(identifier) == false)
{
_eventResult.Add(identifier, notificationResult);
_resetEvents[identifier].Set();
}
}, options.ClearFromHistory, options.AllowTapInNotificationCenter);
Further update: we have been using a badly-outdated version of the Toast.Forms.Plugin plugin. We're running some new tests to see if the latest has conflicts.
Update: After migrating to the latest version of Toast.Forms.Plugin, the issue is still occurring.
So it would indeed seem that the plugin is replacing the App Center delegate with its own. Unfortunately, I don't have a recommendation as to what the best plugin for this scenario is.
I also found this issue: https://github.com/EgorBo/Toasts.Forms.Plugin/issues/164. It seems to be an issue within the plugin that other, non App Center users have faced.
@achocron for the time being, we've ripped out the Toasts plugin and used the AppCenter.Push guidance to handle foreground notifications another way.
We do still have use cases where displaying a native local notification would be useful. Any guidance re: local notifications, Xamarin.Forms, and AppCenter.Push would be most appreciated.
At the moment, I'm having problems with my certificates so I can't test this out myself, but I'd try adding something like this to the iOS project:
Create a new class:
public class NotificationDelegate : UNUserNotificationCenterDelegate
{
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
completionHandler();
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}
}
Add this to the AppDelegate
class:
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
// Add this before Xamarin.Forms.Forms.Init()
UserNotifications.UNUserNotificationCenter.Current.Delegate = new NotificationDelegate();
Xamarin.Forms.Forms.Init();
// Other initialization code...
}
You will still be able to use Push.PushNotificationReceived += OnGotPush;
. When App Center Push sets its custom delegate, it should detect that you have added your own custom delegate already, and invoke it when it receives a notification.
I referred to this Stack Overflow question; you may find it useful as well.
This delegate is documented in the AppCenter docs, there are some additional details for proper use. But I guess it isn't an initial case.
@eliaspuurunen The UserNotificationCenterDelegate
delegate and its WillPresentNotification
form the Toast library is most probably eclipsing the DidReceiveRemoteNotification
method of the UIApplicationDelegate
used by the SDK. WillPresentNotification
wins over DidReceiveRemoteNotification
when both are implemented. A solution is to implement and register your own UserNotificationCenterDelegate
and call the SDK push callback manually from there as mentioned in the documentation @MatkovIvan linked. Then we still have to make sure the Toast library handle the scenario where the customer registers his own UserNotificationCenterDelegate
delegate.
I'll go ahead and close this issue for now, I'll create a bug on the AppCenter side to see if we can circumvent that situation from the SDK perspective.
Thanks for reporting this!
Description
Platform: iOS, Xamarin Forms (.NET Standard)
On Xamarin Forms (iOS only), the Push.PushNotificationReceived event only fires for the first push notification received. Subsequent push notifications, either when the app is in the foreground or background, do not fire the event even if the notification is tapped.
We have also tried disabling method swizzling (as per https://docs.microsoft.com/en-us/appcenter/sdk/push/xamarin-ios#disable-automatic-forwarding-of-application-delegates-methods-to-app-center-services) and noticed that DidReceiveRemoteNotification would not fire after 1st push received.
Enabling/disabling Background Notification in info.plist does not seem to have an effect.
Repro Steps
Please list the steps used to reproduce your issue.
Details
AppCenter.LogLevel = LogLevel.Verbose
before your call toAppCenter.Start(...)
and include the logs here:Note: In this log, five push notifications were sent. The event fired twice as I had some breakpoints enabled. The remaining 3 did not trigger the event.