TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Firebase notifications not working on IOS #223

Closed SteveSitekitcare closed 10 months ago

SteveSitekitcare commented 11 months ago

Hey

I have a working sample for android but the same sample wont work for IOS. Its a dotnet 7 app.

I have followed this guide, with a few modifications from the main documentation in this repo. https://cedricgabrang.medium.com/firebase-push-notifications-in-net-maui-ios-2f4388bf1ac

My sample can be found here https://github.com/SteveSitekitcare/Samples/tree/firebase-notifications-dotnet7

Simply add the plist file from firebase and make sure the certificates are setup correctly

I used postman to push notifications which were succesfull for android but fail for IOS with the following error "InvalidRegistration" or "NotRegistered" image image

Things to note:

Can someone help. ive been at this for weeks

Cheers Steve

LawrenceWlt commented 11 months ago

I have been struggling for weeks too with the same issue. It works fine for Android but not on iOS. I have tried on a physical device and even pushed to TestFlight, none work.

LawrenceWlt commented 11 months ago

I even used WillFinishLaunching, it didn't work either.

SteveSitekitcare commented 11 months ago

I even used WillFinishLaunching, it didn't work either.

I tried this too. I'm currently trying shiny.net https://github.com/shinyorg/shiny

LawrenceWlt commented 11 months ago

@SteveSitekitcare did you manage to make push notifications work with shiny.net? I have tried it but am not getting FCM token.

TobiasBuchholz commented 11 months ago

Hi @SteveSitekitcare, I have taken a look at your sample project and identified two issues that might be the reason for your app to not receive any push notifications:

  1. Your MauiApp1/Platforms/iOS/Entitlements.plist file contains entries that don't belong there but in the Info.plist. I guess you made a simple copy-paste mistake? The most minimalistic Entitlements.plist that supports push notifications looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>
  1. Why does your MauiApp1.csproj file contain PropertyGroup entries like the following:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
  <ApplicationTitle>FirebaseNotifications</ApplicationTitle>
  <ApplicationId>uk.co.sitekitcare.firebasenotifications</ApplicationId>
</PropertyGroup>

The specified ApplicationId (uk.co.sitekitcare.firebasenotifications) doesn't match the one specified at the one in the topmost PropertyGroup (com.companyname.mauiapp1). Either use the same application id in all the ApplicationId entries or just delete the above stated PropertyGroup items alltogether and make sure you use the same application id as specified in the GoogleService-Info.plist.


After applying those changes I was able to receive push notifications on my physical device with your sample app by using the following curl command:

curl --location 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=<your-api-token-from-firebase-console-cloud-messaging-project-settings>' \
--header 'Content-Type: application/json' \
--data '{
 "to" : "<your-device-fcm-token>",
 "collapse_key" : "type_a",
 "mutable_content": true,
 "notification" : {
     "body" : "body of token notification",
      "title": "title of token notification",
     "badge": 1
 },
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Data"
 }
}'

As a last hint: after reinstalling the app the fcm token might change which causes the 'NotRegistered' response, so make sure to press your "Click me" button a few times to really have the current fcm token. Furthermore you can also take a look at the sample project created by @andyzukunft.

I hope this helps!

SteveSitekitcare commented 11 months ago

Hi @SteveSitekitcare, I have taken a look at your sample project and identified two issues that might be the reason for your app to not receive any push notifications:

  1. Your MauiApp1/Platforms/iOS/Entitlements.plist file contains entries that don't belong there but in the Info.plist. I guess you made a simple copy-paste mistake? The most minimalistic Entitlements.plist that supports push notifications looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>aps-environment</key>
  <string>development</string>
</dict>
</plist>
  1. Why does your MauiApp1.csproj file contain PropertyGroup entries like the following:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
  <ApplicationTitle>FirebaseNotifications</ApplicationTitle>
  <ApplicationId>uk.co.sitekitcare.firebasenotifications</ApplicationId>
</PropertyGroup>

The specified ApplicationId (uk.co.sitekitcare.firebasenotifications) doesn't match the one specified at the one in the topmost PropertyGroup (com.companyname.mauiapp1). Either use the same application id in all the ApplicationId entries or just delete the above stated PropertyGroup items alltogether and make sure you use the same application id as specified in the GoogleService-Info.plist.

After applying those changes I was able to receive push notifications on my physical device with your sample app by using the following curl command:

curl --location 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=<your-api-token-from-firebase-console-cloud-messaging-project-settings>' \
--header 'Content-Type: application/json' \
--data '{
 "to" : "<your-device-fcm-token>",
 "collapse_key" : "type_a",
 "mutable_content": true,
 "notification" : {
     "body" : "body of token notification",
      "title": "title of token notification",
     "badge": 1
 },
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Data"
 }
}'

As a last hint: after reinstalling the app the fcm token might change which causes the 'NotRegistered' response, so make sure to press your "Click me" button a few times to really have the current fcm token. Furthermore you can also take a look at the sample project created by @andyzukunft.

I hope this helps!

Ahh thats pretty embarrassing, my app now pushes to the mobile but I'm getting the following

`2023-11-02 12:17:45.257 Xamarin.PreBuilt.iOS[767:166000] Could not resolve assembly Microsoft.VisualStudio.DesignTools.TapContract, Version=17.0.0.0, Culture=neutral, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/8397066B-0CD3-4CAA-8FF8-F52494648DE3/Documents/MauiApp3.content/Microsoft.VisualStudio.DesignTools.TapContract.dll' or one of its dependencies.

2023-11-02 12:17:45.255 Xamarin.PreBuilt.iOS[767:166000] Could not resolve assembly Xamarin.HotReload.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/8397066B-0CD3-4CAA-8FF8-F52494648DE3/Documents/MauiApp3.content/Xamarin.HotReload.Contracts.dll' or one of its dependencies.

Thread started: #9 2023-11-02 12:17:45.674 Xamarin.PreBuilt.iOS[767:166000] Could not resolve assembly System.Private.DataContractSerialization.resources, Version=7.0.0.0, Culture=en, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/8397066B-0CD3-4CAA-8FF8-F52494648DE3/Documents/MauiApp3.content/System.Private.DataContractSerialization.resources.dll' or one of its dependencies.

Thread started: #10 Thread started: #11 2023-11-02 12:17:45.673 Xamarin.PreBuilt.iOS[767:166000] Could not resolve assembly System.Private.DataContractSerialization.resources, Version=7.0.0.0, Culture=en-GB, PublicKeyToken=null. Details: Could not load file or assembly '/var/mobile/Containers/Data/Application/8397066B-0CD3-4CAA-8FF8-F52494648DE3/Documents/MauiApp3.content/System.Private.DataContractSerialization.resources.dll' or one of its dependencies.

[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.RegisterForRemoteNotifications() at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.Initialize() at Plugin.Firebase.Bundled.Platforms.iOS.CrossFirebase.Initialize(CrossFirebaseSettings settings, Options firebaseOptions, String name) at MauiApp1.MauiProgram.<>c.b1_4(UIApplication app, NSDictionary launchOptions) in C:\Git\Sandbox\MauiApp1\MauiProgram.cs:line 46 at Microsoft.Maui.MauiUIApplicationDelegate.<>c__DisplayClass6_0.b1(FinishedLaunching del) at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[FinishedLaunching](IServiceProvider services, Action1 action) at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions) at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) at MauiApp1.Program.Main(String[] args) in C:\Git\Sandbox\MauiApp1\Platforms\iOS\Program.cs:line 13 at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, Span1 args, BindingFlags invokeAttr)' The app has been terminated.`

Did a quick search and I think its a Visual Studio Issue

https://github.com/dotnet/maui/issues/17189 https://github.com/dotnet/maui/issues/14844

I disabled hot reload I just get this

[0:] An error occurred: 'Object reference not set to an instance of an object.'. Callstack: ' at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.RegisterForRemoteNotifications() at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.Initialize() at Plugin.Firebase.Bundled.Platforms.iOS.CrossFirebase.Initialize(CrossFirebaseSettings settings, Options firebaseOptions, String name) at MauiApp1.MauiProgram.<>c.<RegisterFirebaseServices>b__1_4(UIApplication app, NSDictionary launchOptions) in C:\Git\Sandbox\MauiApp1\MauiProgram.cs:line 46 at Microsoft.Maui.MauiUIApplicationDelegate.<>c__DisplayClass5_0.<WillFinishLaunching>b__0(WillFinishLaunching del) at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[WillFinishLaunching](IServiceProvider services, Action1 action) at Microsoft.Maui.MauiUIApplicationDelegate.WillFinishLaunching(UIApplication application, NSDictionary launchOptions) at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) at MauiApp1.Program.Main(String[] args) in C:\Git\Sandbox\MauiApp1\Platforms\iOS\Program.cs:line 13 at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, Span1 args, BindingFlags invokeAttr)' The app has been terminated.

The app breaks on the CrossFirebase.Initialize(CreateCrossFirebaseSettings()); line image

app is set but launchOptions is null

UPDATE1: This occurs on my windows 11 machine, I tested it on a windows 10 machine and it builds and runs, the problem is I only have a apple simulator setup on the windows 10 machine and it doesnt seem to recieve any push notifications in debug or without debug

UPDATE2: I tried building the playground solution on both machines and had the same issue on both image

SteveSitekitcare commented 10 months ago

Hey @TobiasBuchholz

  1. Do you have any instructions on setting up the IOS provision profile?
  2. How do you setup your iphone to test it? e.g. adhoc, emulator?

Still struggling with this and its going to be a show stopper for my project if I can get push notifications working.

Cheers

TobiasBuchholz commented 10 months ago

Hi @SteveSitekitcare,

  1. No, I don't have any instructions on setting up the iOS provisioning profile, because there are plenty tutorials available on the web already, like Push Notifications in iOS from Microsoft or this video by Gerald Versluis.

  2. For testing push notifications I'm using a real iPad Pro 11 device and deploy my apps directly onto it, which works fine, also in debug mode, so nothing special here either.

Hope this helps :)

SteveSitekitcare commented 10 months ago

Hi @SteveSitekitcare,

  1. No, I don't have any instructions on setting up the iOS provisioning profile, because there are plenty tutorials available on the web already, like Push Notifications in iOS from Microsoft or this video by Gerald Versluis.
  2. For testing push notifications I'm using a real iPad Pro 11 device and deploy my apps directly onto it, which works fine, also in debug mode, so nothing special here either.

Hope this helps :)

Appreciate the help, 1 last question (hopefully) What OS do you use to develop on e.g. windows 11, MacOS?

TobiasBuchholz commented 10 months ago

Sure, I'm developing on MacOS with an Apple M1 Pro Chip

SteveSitekitcare commented 10 months ago

Sure, I'm developing on MacOS with an Apple M1 Pro Chip

From trying for a month, there seems to be for too many issues with Windows (specifically windows 11) and everyone who has this working who I have spoke to seems to using a mac.

My issues all seem to be related to file path length, which mac doesnt suffer from.

Ive requested a Mac, I'll feedback here on my results.

Thankyou for the help