Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to implement APNS with Xamarin.Mac?

HHInventiHHInventi Member ✭✭

So I'm maintaining a Xamarin.Forms app currently published on iOS, Android and UWP. My users now would like to have the app ported to Mac. I've tried to load the general Xamarin.Forms project by a Xamarin.Mac project and it kind of works. Doesn't look too good and some of the code is crashing but that's not the point of this question really as I've decided to roll a Xamarin.Mac project which has its own kinds of trouble apparently.

I've created Xamarin.Mac project and done nothing but called NSApplication.Shared.RegisterForRemoteNotification() and I've then overriden RegisteredForRemoteNotifications and FailedToRegisterForNotifications. In those I just currently do a Console.WriteLine to see which function is hit.

I've added com.apple.developer.aps-environment of type with a value of 'development' to the Entitlements.plist file.

Currently when I run the project it starts but none of my WriteLines are hit. I even put a Console.WriteLine into DidFinishLaunching which is not hit either but the app does start. I do get a window on the screen. An empty window as expected as I did not do any work on Main.Storyboard yet but neither of my debug WriteLines are hit.

What am I missing?

Best Answer

Answers

  • HHInventiHHInventi Member ✭✭

    For some reason Console.WriteLine no longer outputs to the Application Output window even though I'm running in Debug config. I'm quite sure it worked at some point when I was playing around with something else.

    Oh well I converted my Logging class to use System.Diagnostics.Debug.WriteLine instead and that shows up in the output window but it hit the Failed override function.

    The message is: FailedToRegisterForRemoteNotifications: Error Domain=NSOSStatusErrorDomain Code=13 "Bundle identifier mismatch" UserInfo={NSDebugDescription=Bundle identifier mismatch}

    I've struggled with this message before when Console.WriteLine worked but never found a solution.

  • HHInventiHHInventi Member ✭✭

    Updated VS for Mac 2019 to the latest on the Preview channel.

    Now if I start the app without the Entitlements section I get this error: FailedToRegisterForRemoteNotifications: Error Domain=NSOSStatusErrorDomain Code=13 "Application not properly entitled for push notifications." UserInfo={NSDebugDescription=Application not properly entitled for push notifications.}

    If I insert com.apple.developer.aps-environment of type with a value of 'development' to the Entitlements.plist file the app will not build. I get this dialog: Unable to launch application to debug, possibly due to it crashing quickly.
    See https://aka.ms/xm-crash-no-output for more help.
    Could not open /Users/smartdeveloper/Desktop/SMARTaccess/SMARTaccess.mac/bin/Debug/SMARTaccess.app: pid returned -1

  • HHInventiHHInventi Member ✭✭

    From About Visual Studio dialog. Hope this is useful for someone with some info here.

    === Visual Studio Enterprise 2019 for Mac (Preview) ===

    Version 8.7 Preview (8.7 build 1935)
    Installation UUID:
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

    Package version: 612000082
    

    === Mono Framework MDK ===

    Runtime:
    Mono 6.12.0.82 (2020-02/109b4b6f445) (64-bit)
    Package version: 612000082

    === Roslyn (Language Service) ===

    3.7.0-4.20330.10+1f3fa3ff7330cf6d2631a230e42e93a85aa7d338

    === NuGet ===

    Version: 5.7.0.6653

    === .NET Core SDK ===

    SDK: /usr/local/share/dotnet/sdk/3.1.302/Sdks
    SDK Versions:
    3.1.302
    3.1.301
    3.1.200
    3.1.102
    3.1.101
    3.0.100
    2.1.701
    2.1.700
    MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Sdks

    === .NET Core Runtime ===

    Runtime: /usr/local/share/dotnet/dotnet
    Runtime Versions:
    3.1.6
    3.1.5
    3.1.2
    3.1.1
    3.0.0
    2.1.20
    2.1.19
    2.1.17
    2.1.16
    2.1.15
    2.1.13
    2.1.12
    2.1.11

    === Xamarin.Profiler ===

    Version: 1.6.12.29
    Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

    === Updater ===

    Version: 11

    === Xamarin.Android ===

    Not Installed

    === Microsoft OpenJDK for Mobile ===

    Java SDK: Not Found

    Android Designer EPL code available here:
    https://github.com/xamarin/AndroidDesigner.EPL

    === Android SDK Manager ===

    Version: 16.7.0.13
    Hash: 8380518
    Branch: remotes/origin/d16-7
    Build date: 2020-07-01 01:57:56 UTC

    === Android Device Manager ===

    Version: 16.7.0.18
    Hash: 4b44bc1
    Branch: remotes/origin/d16-7
    Build date: 2020-07-01 01:58:18 UTC

    === Apple Developer Tools ===

    Xcode 11.5 (16139)
    Build 11E608c

    === Xamarin.Mac ===

    Version: 6.20.1.44 (Visual Studio Enterprise)
    Hash: 28cab0985
    Branch: d16-7
    Build date: 2020-07-07 15:04:11-0400

    === Xamarin.iOS ===

    Version: 13.20.1.44 (Visual Studio Enterprise)
    Hash: 28cab0985
    Branch: d16-7
    Build date: 2020-07-07 15:04:12-0400

    === Xamarin Designer ===

    Version: 16.7.0.486
    Hash: d92fc32c1
    Branch: remotes/origin/d16-7
    Build date: 2020-06-24 00:37:00 UTC

    === Build Information ===

    Release ID: 807001935
    Git revision: e12917353971d3d746cb74a390d1c78338cb5255
    Build date: 2020-07-14 16:30:19-04
    Build branch: release-8.7
    Xamarin extensions: e12917353971d3d746cb74a390d1c78338cb5255

    === Operating System ===

    Mac OS X 10.15.5
    Darwin 19.5.0 Darwin Kernel Version 19.5.0
    Tue May 26 20:41:44 PDT 2020
    root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Here is the respective documentation from Apple on those APIs: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/HandlingRemoteNotifications.html

    And here is a 3rd party write up: https://www.raywenderlich.com/8164-push-notifications-tutorial-getting-started

    As noted in the FAQ:

    Often you'll need to convert objective-c stack overflow / tutorials / apple documentation to C# and this documentation will help:

    https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/
    https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/

    I have not used the respective API, you may want to compare against the Apple documentation to make sure you have:

    • The correct entitlement (looks like you may need one?)
    • Any info.plist keys required
    • Overriding the correct selectors.
  • HHInventiHHInventi Member ✭✭

    The Xamarin.Forms app is already capable of handling push notifications for iOS, Android and UWP on Windows so all of the background stuff works. I just now need to port the project over to Mac OS and make notifications work on that platform too. That is: register the device with APNs so I can get the deviceToken which I will then send to Azure which I use as the general push notification hub for all platforms. But getting the device to register for APNs is the same process whether I use Apple directly or the general Azure Push Notification service. Behind the scenes Azure will notify APNs to notify my device when need be. But to get that going I need a deviceToken which is returned to me by the RegisteredForRemoteNotifications method if only I could get it to register correctly.

    I've read through all of your links but they did not provide an answer to why my project currently does not accept my certificates and provisioning profiles and seems to not be able to accept APNs registration and return a valid deviceToken. This is weird. I've also tried to create a Native Cocoa App with XCode in Objective-C where I was actually able to get a valid deviceToken but I would much rather stay in the C# Xamarin family and reuse my libraries for communicating with different web services to handle registering at Azure and general app logic. Dont want to rewrite all that code to Objective-C even though I fairly comfortable with Objective-C.

    You said I might need another entitlement?

    I've already added 'com.apple.developer.aps-environment' with a string value of 'development'. That is the only one I've been able to find that I need to have.

  • HHInventiHHInventi Member ✭✭

    I dont believe anything. I just notice something not working. I have no idea what it is that is not working.

    I've begone working on a Native Cocoa app with XCode to get going. Thanks.

Sign In or Register to comment.