I have developed MAUI app using Visual Studio 2022 v 17.14.16 and .NET 9. I have already developed Android app and it is on Google Play Store.
Now I am developing iOS MAUI app so I have enabled iOS as a target Platform. Target .Net runtime as .Net 9 and Minimum target iOS framework is 16.0.
I have implemented Push Notification functionality by adding Notification Service Extension (NSE) Project in Visual Studio Windows.
For both Main app and NSE, I have created App Id. For Main App, App Id I have enabled Push Notification and App Group capabilities and for NSE project I have enabled App Group capability in Apple developer portal. I have created App group (group.com.company.id.name) and configured to App Id of both Main App and NSE project.
In App.xaml.cs file, I have initialized OneSignal like below:
OneSignal.Initialize("OneSignal_App_Id");
OneSignal.Notifications.RequestPermissionAsync(true);
In Main app’s Info.plist, I have added below keys for Push notification-
Main app Info.plist
LSRequiresIPhoneOS
UIDeviceFamily
1
UIRequiredDeviceCapabilities
arm64
UISupportedInterfaceOrientations
UIInterfaceOrientationPortrait
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
UISupportedInterfaceOrientations~ipad
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
XSAppIconAssets
Assets.xcassets/appicon.appiconset
CFBundleShortVersionString
0.1
CFBundleVersion
1.0.0
CFBundleIdentifier
com.company.id.name
CFBundleDisplayName
Mobile app
CFBundleName
Mobile app
MinimumOSVersion
16.0
NSCameraUsageDescription
We need camera access to capture images.
CFBundleURLTypes
CFBundleURLSchemes
com.googleusercontent.apps.reverseClientId
UIBackgroundModes
fetch
remote-notification
OneSignal_app_groups_key
group.com.company.id.name.onesignal
Main app’s Entitlements.plist
aps-environment
development
com.apple.security.application-groups
group.com.company.id.name.onesignal
Main App .csproj Configuration
net9.0-android35.0;net9.0-ios18.0
Exe
true
true
enable
16.0
true
false
None
Platforms\iOS\Entitlements.plist
true
NSE Info.plist
CFBundleDisplayName
OneSignalNotificationServiceExtension
CFBundleName
OneSignalNotificationServiceExtension
CFBundleIdentifier
com.company.id.name.OneSignalNotificationServiceExtension
CFBundlePackageType
XPC!
CFBundleShortVersionString
0.1
CFBundleVersion
1.0.0
NSExtension
NSExtensionPointIdentifier
com.apple.usernotifications.service
NSExtensionPrincipalClass
NotificationService
UIBackgroundModes
remote-notification
MinimumOSVersion
16.0
OneSignal_app_groups_key
group.com.company.id.name.onesignal
NSE Entitlements.plist
com.apple.security.application-groups
group.com.company.id.name.onesignal
NSE configuration
net9.0-ios18.0
Library
enable
true
16.0
True
ios-arm64
com.company.id.name.OneSignalNotificationServiceExtension
Entitlements.plist
Entitlements.plist
NotificationServiceClass
using System;
using Foundation;
using OneSignalSDK.DotNet;
using OneSignalSDK.DotNet.iOS;
using UIKit;
using UserNotifications;
namespace OneSignalNotificationServiceExtension
{
[Register("NotificationService")]
public class NotificationService : UNNotificationServiceExtension
{
Action ContentHandler { get; set; }
UNMutableNotificationContent BestAttemptContent { get; set; }
UNNotificationRequest ReceivedRequest { get; set; }
protected NotificationService(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic,
// it only exists so that the OS can instantiate an instance of this class.
}
public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action contentHandler)
{
Console.WriteLine("DidReceive**********************");
ReceivedRequest = request;
ContentHandler = contentHandler;
BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();
NotificationServiceExtension.DidReceiveNotificationExtensionRequest(request, BestAttemptContent, contentHandler);
}
public override void TimeWillExpire()
{
NotificationServiceExtension.ServiceExtensionTimeWillExpireRequest(ReceivedRequest, BestAttemptContent);
ContentHandler(BestAttemptContent);
}
}
}
Below is my payload of notification I am passing via API:
var notification = new Notification(appId: _AppId)
{
Contents = new StringMap(en: message),
IncludeExternalUserIds = externalUserId,
IosBadgeType = "Increase",
IosBadgeCount = 1,
MutableContent = true
};
In both Main app and NSE project properties iOS Bundle signing, I have selected Manual Provisioning and selected Signing Identity and Provisioning Profile resp. for Main App and NSE
I have paired Visual Studio Windows with Mac. I am getting Push notifications on my app. On Notification Center it is showing correct count but issue is on app icon badge count is not increasing, it is stuck at 1 only.
I think NSE is not working because if I add any prefix to body in DidReceiveNotification method, then it is not showing.
I am getting below error:
INFO: An error occurred while forwarding HotReload local tunnel: System.IO.IOException: USB connect timeout (ENODATA), likely because the app failed to launch. Please review device logs and/or crash reports for more information.
at Xamarin.MacDev.AggregateAsyncResult.CheckError(Boolean cancelled)
at Xamarin.MacDev.IPhoneDevice.EndConnectStream(IAsyncResult result)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
at Xamarin.Messaging.IDB.IPhoneUsbLocalTunnelConnection.StartLocalTunnelAsync()
at Xamarin.Messaging.IDB.LaunchAppMessageHandler.ForwardLocalTunnelsAsync(LaunchAppMessage, IPhoneDevice)
ERROR: [iOS HotReload] Failed to connect to "iPhone" over USB on port 11000.
For this error- I have disabled / unchecked .NET Hot Reload and uncheck Enable Hot Reload but same error persists.
Is there anything I am missing because of which badge count is not increasing? If there is no communication happens between Main app and NSE then what needs to be done?
