local notification in ios10 using "UNUserNotificationCenter"

ChristianReicheltChristianReichelt ✭✭DEMember ✭✭
edited October 2016 in Xamarin.Forms

Hi

I'm trying to use the new notification center of ios10.
Switching from UIApplication.SharedApplication.ScheduleLocalNotification(note) to UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
...

Debugging shows that message is actually sent but it calls "ReceivedLocalNotification" instead of "WillPresentNotification" from UNUserNotificationCenterDelegate.

So some of my properties are missing and I can't show the message correct.

Is there a missing dependency in forms or is the functionality missing because of "FormsApplicationDelegate"?

Anyone else managed to get it working?

Answers

  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    I spent the last couple days implementing the UserNotification framework on my app. Here's my thoughts:

    1. Is the device you're testing with running iOS 10? The new framework is not backwards-compatible.

    2. How are you doing the delegate? Implementing the IUNUserNotificationDelegate interface on your AppDelegate requires some extra work registering the methods with the system. Much easier to subclass the UNUserNotificationDelegate object and override its methods.

  • ChristianReicheltChristianReichelt ✭✭ DEMember ✭✭

    hey thanks for your answer.

    1. yes I'm working on an iOS 10 device. In debug but that shouldn't make a difference, right?
    2. I followed the xamarin tutorial and created a new subclass
      public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate { public UserNotificationCenterDelegate() { } public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) { completionHandler(UNNotificationPresentationOptions.Alert); }}

    that's why I'm wondering that it's not even calling the action

  • JoeMankeJoeManke ✭✭✭✭✭ USMember ✭✭✭✭✭

    Your delegate looks fine. Could you post the code where you create and send the notification?

  • ChristianReicheltChristianReichelt ✭✭ DEMember ✭✭

    Sure.
    DependencyService:
    internal class LocalNotificationService : ILocalNotificationService { public void Notify(LocalNotification notification) { var content = new UNMutableNotificationContent { Title = notification.Title, Subtitle = "subtitle", Body = notification.Text, Badge = 1 };

        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "localNotification";
    
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
        {
            if (err != null)
            {
                Debug.WriteLine(err);
            }
        });
    
        }
    }`
    

    MessageCall looks this way, but it's just the dependency call:
    DeviceManager.SendLocalNotification(new LocalNotification() { Title = "HNR Kinsmen App", Text = "Message Arrived ", Id = 1, NotifyTime = DateTime.Now });

  • ChristianReicheltChristianReichelt ✭✭ DEMember ✭✭

    Just figured out, that it's same behavior when I'm creating a clean Xamarin.iOS project and following the documentation.
    Did anyone trying this new feature?

  • JorgeMauricioJorgeMauricio BRMember

    Did anyone get this feature working ok?

  • ChristianReicheltChristianReichelt ✭✭ DEMember ✭✭

    yes, it's working for me now... There was an entry missing in the docs.
    But with the recent version it should work fine.

    What's your problem?

  • Agg94Agg94 ✭✭ ESMember ✭✭

    Hi all,

    I have followed the docs step by step and it works perfect with a non-repeating local notification:

    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);

    But, once I try to make it repeatable:

    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, true);

    the debugger freezes and on the output i get:

    *** Assertion failure in -[UNTimeIntervalNotificationTrigger _initWithTimeInterval:repeats:]

    Has anyone met this problem?

  • Agg94Agg94 ✭✭ ESMember ✭✭

    Ok, so the problem in my case was that the time interval specified in the CreateTrigger method is in seconds and it must be higher than some value (I couldn't find it).

    But, for example:

    //Local notification repeating every 10 minutes
    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(10*60, true);
    

    works perfect if you follow the docs step by step.

  • ChrisColeTechChrisColeTech ✭✭✭ USMember ✭✭✭

    I got this working also. it was my most proud accomplishment for the week.

  • SipkeSchoorstraSipkeSchoorstra ✭✭ NLMember ✭✭

    I get the same issue as @Agg94 when using repeat = true:

    *** Assertion failure in -[UNTimeIntervalNotificationTrigger _initWithTimeInterval:repeats:]

Sign In or Register to comment.