urbanairship/urbanairship-xamarin

Delegates Not Working?

tristanl-slalom opened this issue ยท 11 comments

I'm trying to incorporate Urban Airship into an iOS project, and things are working pretty well. I'm receiving notifications when the app isn't running. But when the app is running, my UAPushNotificationDelegate object is not getting notified of notifications, and neither is my UARegistrationDelegate.

Here is what happens when a notification is received while the app is in the foreground.

2016-11-23 15:42:09.624 LoanDepot[4683:1190060] [D] +[UAAppIntegration userNotificationCenter:willPresentNotification:withCompletionHandler:] [Line 136] Notification center will present notification: <UNNotification: 0x170a29a20; date: 2016-11-23 23:42:09 +0000, request: <UNNotificationRequest: 0x170a26360; identifier: 5851990D-D479-42F2-8EC0-D41DA2BBDE9B, content: <UNNotificationContent: 0x1704e1100; title: It's Working!, subtitle: (null), body: This is a fantastic alert indeed., categoryIdentifier: ua_yes_no_foreground, launchImageName: , peopleIdentifiers: (
), threadIdentifier: , attachments: (
), badge: 3, sound: (null), hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNPushNotificationTrigger: 0x170209520; contentAvailable: NO, mutableContent: NO>>>
2016-11-23 15:42:09.626 LoanDepot[4683:1190060] [I] +[UAAppIntegration handleIncomingNotification:foregroundPresentation:completionHandler:] [Line 228] Received notification: {
    "_" = "756a2381-573e-4157-aea7-7a90b7c6999c";
    aps =     {
        alert =         {
            body = "This is a fantastic alert indeed.";
            title = "It's Working!";
        };
        badge = 3;
        category = "ua_yes_no_foreground";
    };
}
2016-11-23 15:42:09.626 LoanDepot[4683:1190060] [D] +[UAActionRunner runActionWithName:value:situation:metadata:completionHandler:] [Line 84] No action found with name aps, skipping action.
2016-11-23 15:42:09.627 LoanDepot[4683:1190060] [D] +[UAActionRunner runActionWithName:value:situation:metadata:completionHandler:] [Line 84] No action found with name _, skipping action.

And here is my code setting up the UAirship.Push object.

var config = UAConfig.DefaultConfig();
UAirship.TakeOff(config);
UAirship.Push.UserPushNotificationsEnabled = true;
UAirship.Push.BackgroundPushNotificationsEnabled = true;
UAirship.Push.PushNotificationDelegate = new PushNotificationSupport();
UAirship.Push.RegistrationDelegate = new RegistrationSupport();
return true;

Again, we're definitely getting notifications when the app is in the background, and they open the app and do everything I expect them to. But when the app is in the foreground, I have no idea the notifications are happening. What am I missing?

@trisco2001 We store weak references to the delegate. You need to store a strong reference to the delegates as members on your class.

Ack!! Mind = Blown. ๐Ÿ‘

Longtime C# and iOS developer, and still did not see that one coming. Hahah.

Looks like xamarin provides a mechanism to make delegates strong by default - https://developer.xamarin.com/guides/cross-platform/macios/binding/binding-types-reference/#WrapAttribute

Ill look into adding that in the next release.

@trisco2001 looks like the wrap delegate is in place since 3.1. What version are you using?

Version 3.1.1. I just got it out of the Xamarin Component Store within the last week or two.

@trisco2001 Did you ever getting this working? I am seeing the same thing, the notifications come through in the logs, but not sure how or where to handle that callback.

Thanks

@mattkrebs You will need to keep a strong reference somewhere to the delegates. We are still looking into fixing this but its unclear to us why its not working. We hope to have it fixed in the next few weeks.

@rlepinski Do you think you provide an example on how to do this?

Store a reference to the delegate as a field on a class:

namespace Sample
{
	[Register ("AppDelegate")]
	public class AppDelegate : UIApplicationDelegate
	{
		private UAPushNotificationDelegate delegate;

		public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
		{
			// Bootstrap the Urban Airship SDK
			UAirship.TakeOff ();


			// Store a strong reference
			delegate = new MyDelegate();

			// Assign it
			UAirship.Push.PushNotificationDelegate = delegate;

			return true;
		}
	}

@mattkrebs Yeah, sorry I missed this. Just like @rlepinski above. Without it, the delegate is seemingly garbage collected when it falls out of scope and never receives the message. A simple fix that I would have ultimately put in place naturally if my testing had worked, but since my tests never worked, I never moved on. :)

@trisco2001 & @rlepinski Ah...thank you so much! Got it working.