Shaboboo desires to attract extra consideration to this query:
I’m fairly certain the difficulty is that the features inside the UNUserNotificationCenterDelegate will not be getting hit. I need assistance discovering out why the delegate just isn’t working as anticipated. This app must be launched quickly!
I’ve a maui .net7 iOS software and I can not seem to get the push notifications to work fairly proper when the app just isn’t within the foreground. (Word, I am NOT utilizing Firebase.)
I’ve been following Microsoft’s documentation: https://be taught.microsoft.com/en-us/xamarin/ios/platform/user-notifications/enhanced-user-notifications?tabs=home windows
and this code instance right here https://github.com/xamarin/xamarin-forms-samples/blob/97d6825e2ba3aa64667695f3c43224c9171e51e5/LocalNotifications/LocalNotifications/LocalNotifications.iOS/iOSNotificationReceiver.cs
Situation 1
When the app is within the foreground when a push notification is available in, the notification is recieved within the appDelegate’s ReceivedRemoteNotification operate. I then put the notification data into my native database, and show a neighborhood notification utilizing the plugin Plugin.LocalNotification; This notification shows appropriately and when clicked the appliance will open a specific view.
All good right here.
Situation 2
When the app is operating however within the background, the identical happens as when within the foreground. The notification is logged within the database, a neighborhood notification is proven that does the proper motion when clicked.
Nevertheless, it is ALSO exhibiting one other “default” notification that does nothing when clicked.
I would like to forestall this “default” notification from exhibiting. Nowhere in my code do I name the code so as to add the nofication: UNUserNotificationCenter.Present.AddNotificationRequest(request, OnNotificationError);
Situation 3
When the app just isn’t operating and a notification is available in, the app exhibits the “default” notification that does nothing when clicked, and the notification just isn’t logged in my database.
I would like to make sure the code so as to add the notification to the database runs. When the app is closed it will not must open a specific view because the app would require the consumer to log in, however I do want the app to current the notification within the GUI to allow them to nonetheless attain the proper view.
Debugging on the mac iOS simulator just isn’t working for me (notifications will not be showing in any respect), so I’ve been making an attempt to make use of appcenter analytics logUsage() to find out what code is getting hit. It appears the logging just isn’t occurring whereas the app is within the background, so its tough to say if the UNUserNotificationCenterDelegate’s WillPresentNotification and DidReceiveNotificationResponse code it getting hit or not. Looks as if they don’t seem to be.
AppDelegate.cs
public override bool FinishedLaunching(UIApplication software, NSDictionary launchOptions)
{
UNUserNotificationCenter.Present.Delegate = new UserNotificationCenterDelegate(); //my customized class
// … code to register to register for notifications and verify permission – works, eliminated for instance
}
[Export(“application:didReceiveRemoteNotification:”)]
public void ReceivedRemoteNotification(UIApplication software, NSDictionary notification)
{
//code to course of notification, add to DB and show the native notification begins right here.
//Runs appropriately when app is in foreground
}
UserNotificationCenterDelegate.cs
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate, IUNUserNotificationCenterDelegate
{
[Export(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]
public override void WillPresentNotification(UNUserNotificationCenter heart, UNNotification notification, Motion<UNNotificationPresentationOptions> completionHandler)
{
//tried including to DB right here for when the appliance just isn’t operating, didn’t work. Unsure this code is ever hit
completionHandler(UNNotificationPresentationOptions.None); //this could inform the app to not present the default notification
}
[Export(“userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:”)]
public override void DidReceiveNotificationResponse(UNUserNotificationCenter heart, UNNotificationResponse response, Motion completionHandler)
{
//this ought to be hit when the default notification is clicked. Unsure that is ever hit
if (!response.IsDismissAction)
{
//run code to open the proper view
}
completionHandler();// Inform caller it has been dealt with
}
}
Can anybody inform me:
the place the distant notifications come into the app whereas within the background, so I can guarantee the info is written to the DB. (doesn’t appear to be AppDelegate’s ReceivedRemoteNotification)
the place I can decide if the app ought to show the “default” notification when the app is within the background (UNUserNotificationCenterDelegate’s WillPresentNotification is not doing it)
the place I ought to write the code to open the proper view when the default notification is clicked (UNUserNotificationCenterDelegate’s DidReceiveNotificationResponse is not doing it. Won’t be required if I can get #1 & 2 working appropriately)
Many thanks!
Replace:
I found one other operate not talked about within the Microsoft documentation:
[Export(“application:didReceiveRemoteNotification:fetchCompletionHandler:”)]
public void DidReceiveRemoteNotification(UIApplication software, NSDictionary notification, Motion<UIBackgroundFetchResult> completionHandler)
{
//code to jot down the notification to the DB and show the native notification
completionHandler(UIBackgroundFetchResult.NewData);
}
the system calls this methodology when your app is operating within the foreground or background.
Tells the app {that a} distant notification arrived that signifies there’s information to be fetched.
Use this methodology to course of incoming distant notifications in your app.
See apple’s documentation:
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application
Additionally talked about right here:
https://github.com/dotnet/maui/points/6259
Now when the app just isn’t operating, I can write the notification into the DB and present the native notification!
It additionally exhibits the default notification; I nonetheless want to find out the way to forestall this notification from showing when the app is within the background or not operating.