Bug 43575 - UNNotificationServiceExtension is not being triggered
Summary: UNNotificationServiceExtension is not being triggered
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.99 (iOS 10 previews)
Hardware: Macintosh Mac OS
: High major
Target Milestone: Xcode8 (iOS10)
Assignee: Alex Soto [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-08-21 17:18 UTC by Konstantin
Modified: 2016-09-01 07:11 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Here's 2 hello-worlds, one of them in C# (xamarin) and it doesn't work. The second one is Swift and it works fine. And I don't see any obvious difference in them (253.79 KB, application/zip)
2016-08-21 17:18 UTC, Konstantin
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Konstantin 2016-08-21 17:18:28 UTC
Created attachment 17149 [details]
Here's 2 hello-worlds, one of them in C# (xamarin) and it doesn't work. The second one is Swift and it works fine. And I don't see any obvious difference in them

Hi,

Apologizes, if it's already been discussed: I'm working on new rich notification experience for iOS 10 and stuck: I can't make UNNotificationServiceExtension work with Xamarin:

Here's how my test-app's solution look like:
![Solution](http://i.stack.imgur.com/cBN3X.png)
IUNNotificationContentExtension works like a charm, but not UNNotificationServiceExtension.

To add UNNotificationServiceExtension:
 1) I've added "Action Extension" 
 2) Cleaned up project and left only 2 files:
    info.plist:

		<?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>CFBundleDisplayName</key>
			<string>ServiceExtension</string>
			<key>CFBundleName</key>
			<string>ServiceExtension</string>
			<key>CFBundleIdentifier</key>
			<string>com.mobilefootie.fotmobpro.serviceextension</string>
			<key>CFBundleDevelopmentRegion</key>
			<string>en</string>
			<key>CFBundleInfoDictionaryVersion</key>
			<string>6.0</string>
			<key>CFBundlePackageType</key>
			<string>XPC!</string>
			<key>CFBundleShortVersionString</key>
			<string>1.0</string>
			<key>CFBundleVersion</key>
			<string>1.0</string>
			<key>MinimumOSVersion</key>
			<string>10.0</string>
			<key>NSExtension</key>
			<dict>
				<key>NSExtensionPointIdentifier</key>
				<string>com.apple.usernotifications.service</string>
				<key>NSExtensionPrincipalClass</key>
				<string>$(PRODUCT_MODULE_NAME).NotificationService</string>
			</dict>
		</dict>
		</plist>

    NotificationService.cs:

		using System;
		using UserNotifications;

		namespace ServiceExtension
		{
			public class NotificationService : UNNotificationServiceExtension
			{
				private Action<UNNotificationContent> contentHandler;
				private UNMutableNotificationContent bestAttemptContent;

				public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
				{
					this.contentHandler = contentHandler;
					this.bestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();

					if (bestAttemptContent != null)
					{
						bestAttemptContent.Title = "[modified!]";
						contentHandler(bestAttemptContent);
					}
				}

				public override void TimeWillExpire()
				{
					if (contentHandler != null && bestAttemptContent != null)
					{
						contentHandler(bestAttemptContent);
					}
				}
			}
		}


I've tried pretty much everything: re-add reference to ServiceExtension project, played with various NSExtensionPointIdentifier, and so on.

The issue is definitely somewhere on the app-level, because with swift same hello-world works fine, so it not a misconfigured notification, etc.

I've attached the xamarin-proj (including swift-version as well, which is working fine..) to the post.
Any ideas what might be wrong with UNNotificationServiceExtension? Can it be an issue on a framework side?

=== Xamarin Studio ===

Version 6.1 (build 5345)
Xcode: 8.0 beta 5 (8S193k)
Everything seems to be up to date.

Thanks!
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-08-24 10:15:07 UTC
@Alex, can you have a look at this since you implemented user notifications?
Comment 3 Alex Soto [MSFT] 2016-09-01 01:36:43 UTC
Hello Konstantin 

Been trying to reproduce this swift app in ObjC but unfortunately I have not been able to. Is there any chance you could provide a working swift app? It seems it does not build anymore or even better a working ObjC app.
Comment 4 Alex Soto [MSFT] 2016-09-01 07:11:50 UTC
Hello Konstantin 

So I managed to fix this adding two easy changes to you app.

The first and likely most important change is in ServiceExtension -> NotificationService.cs. This file is missing the IntPtr ctor. Since this class will be created by ObjC this ctor is called when that happens so it is very important for it to be there. Please do not depend on this constructor for any initialisation logic or anything really.

https://gist.github.com/dalexsoto/734dc30ef2a697783a2e4db004f1d60a#file-notificationservice-cs-L8-L11

And the second one is to your MainApp -> ViewController.cs. Using the UserNotifications framework you need to ask permission to the user to actually being able to send him any notifications so I added the method EnableNotifications () this has the logic to ask for permission and print some of the app given permissions and settings

https://gist.github.com/dalexsoto/734dc30ef2a697783a2e4db004f1d60a#file-viewcontroller-cs-L20-L46

with the 2 changes mentioned above I am able to run your sample on both device and sim.

Please feel free to reopen the bug if this does not solve your described issue.

Cheers!