Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Xamarin Forms Local Notification App is in Background

$
0
0

i´m facing this problem for several days. Before i dive into code i describe the problem and circumstances.

Testcase: for testing i monitor my gps and set points to get a local notification.

Technical fast path: in xamarin forms i build a local notification dependency to setup plattformspecific the Local notification. Based on location change the trigger sends the local notification

Behaviour: This works great when the App is in foreground or closed less then 10 minutes or something. But when the app is closed for an hour i didn´t get the local notification but my logfile shows me that the local Notification is in queue but will not presented :-(. When i open the app the local notification is presented.

Plattform: iOS 10.3 Circumstances: All authorization points are done (Messagecenter)

AppDeleagte.cs
`void AskForNotification() {

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
    // Ask the user for permission to get notifications on iOS 10.0+
    UNUserNotificationCenter.Current.RequestAuthorization(
            UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
            (approved, error) => { });
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
    // Ask the user for permission to get notifications on iOS 8.0+
    var settings = UIUserNotificationSettings.GetSettingsForTypes(
            UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
            new NSSet());

    UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}

}`

UserNotificationDelegate.cs
`public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public UserNotificationCenterDelegate()
{
}

    #region Override Methods
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do something with the notification
        CrossSimpleLogger.Current.Info("Active Notification: " +  notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert);
        UNUserNotificationCenter.Current.GetPendingNotificationRequests((result) =>
        {
            CrossSimpleLogger.Current.Info("After WillPresentNotification -> PendingNotificationRequests: " +  result.Length);

        });

    }
    #endregion
}`

DependencyLocalNotifiation.cs
`public class DependencyLocalNotification : ILocalNotification
{
public void SendLocalNotification(string id, string title, string subtitle, string body, int badge)
{

        var content = new UNMutableNotificationContent();
        content.Title = title;
        content.Subtitle = subtitle;
        content.Body = body;
        content.Badge = badge;

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

        var requestID = id;

        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);



        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
        {
            CrossSimpleLogger.Current.Info("AddNotificationRequest: " +  request);

            if (err != null)
            {
               CrossSimpleLogger.Current.Info("AddNotificationRequestError: " + err);
               CrossSimpleLogger.Current.Info(err.ToString());
            }

            UNUserNotificationCenter.Current.GetPendingNotificationRequests((result) =>
            {
                CrossSimpleLogger.Current.Info("PendingNotificationRequests: " + result.Length);
                foreach(UNNotificationRequest Request in result){
                    UNNotificationContent _tmp = Request.Content;
                    UNNotificationTrigger _tmptrigger = Request.Trigger;
                   CrossSimpleLogger.Current.Info(_tmp.Title);
                }

            });
        });
    }
}`

ILocalNotification.cs
public interface ILocalNotification { void SendLocalNotification(string id, string title, string subtitle, string body, int badge); }

Geolocator
`private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
var test = e.Position;
CrossSimpleLogger.Current.Info("GPS postion changed");
CrossSimpleLogger.Current.Info("Full: Lat: " + Math.Round(e.Position.Latitude,2).ToString()
+ " Long: " + Math.Round(e.Position.Longitude,2).ToString());

            foreach(FappLocation Location in DemoData.Demolocations) {


                double distanceto = distance(Location.Lat,
                                                  Location.Lng,
                                                  e.Position.Latitude,
                                                  e.Position.Longitude,
                                                 'm');

                if (distanceto < 500)
                {
                    DependencyService.Get<ILocalNotification>().SendLocalNotification(Location.Guid,"Fappolocation " + Location.Name, Math.Ceiling(distanceto) + "m", Location.Beschreibung, 0);
                }
            }
        });
    }

`

logfile
09.06.2017 17:40:53 - GPS postion changed 09.06.2017 17:40:53 - Full: Lat: 48,64 Long: 9,06 09.06.2017 17:40:53 - AddNotificationRequest: <UNNotificationRequest: 0x17143c740; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x170114910; title: Fappolocation zumHolz, subtitle: 33m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x17143c660; repeats: NO, timeInterval: 5.000000>> 09.06.2017 17:40:53 - PendingNotificationRequests: 1 09.06.2017 17:40:53 - Fappolocation ZumHolz 09.06.2017 17:40:53 - GPS postion changed 09.06.2017 17:40:53 - Full: Lat: 48,64 Long: 9,06 09.06.2017 17:40:53 - AddNotificationRequest: <UNNotificationRequest: 0x17143aa00; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x170116f50; title: Fappolocation ZumHolz, subtitle: 74m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x17143a7c0; repeats: NO, timeInterval: 5.000000>> 09.06.2017 17:40:53 - PendingNotificationRequests: 1 09.06.2017 17:40:53 - Fappolocation ZumHolz 09.06.2017 17:40:54 - GPS postion changed 09.06.2017 17:40:54 - Full: Lat: 48,64 Long: 9,06 09.06.2017 17:40:54 - AddNotificationRequest: <UNNotificationRequest: 0x17483afa0; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x174112360; title: Fappolocation ZumHolz, subtitle: 17m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x174a28de0; repeats: NO, timeInterval: 5.000000>> 09.06.2017 17:40:54 - PendingNotificationRequests: 1 09.06.2017 17:40:54 - Fappolocation ZumHolz 09.06.2017 17:40:59 - Active Notification: <UNNotification: 0x17143cfc0; date: 2017-06-09 17:40:59 +0000, request: <UNNotificationRequest: 0x171432c40; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x170116fe0; title: Fappolocation ZumHolz, subtitle: 17m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x171432e00; repeats: NO, timeInterval: 5.000000>>> 09.06.2017 17:40:59 - After WillPresentNotification -> PendingNotificationRequests: 0 09.06.2017 17:41:02 - GPS postion changed 09.06.2017 17:41:02 - Full: Lat: 48,64 Long: 9,06 09.06.2017 17:41:02 - AddNotificationRequest: <UNNotificationRequest: 0x171432d20; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x1701172b0; title: Fappolocation ZumHolz, subtitle: 10m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x17143ad00; repeats: NO, timeInterval: 5.000000>> 09.06.2017 17:41:02 - PendingNotificationRequests: 1 09.06.2017 17:41:02 - Fappolocation ZumHolz

OPEN APP THE THE NOTIFICATION COMES

09.06.2017 17:41:07 - Active Notification: <UNNotification: 0x174a28920; date: 2017-06-09 17:41:07 +0000, request: <UNNotificationRequest: 0x174a28820; identifier: e26157ca-bce3-48b4-908e-a95eaac591cf, content: <UNNotificationContent: 0x1741134d0; title: Fappolocation ZumHolz, subtitle: 10m, body: Chillig, categoryIdentifier: , launchImageName: , peopleIdentifiers: ( ), threadIdentifier: , attachments: ( ), badge: 0, sound: (null), hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x17403b040; repeats: NO, timeInterval: 5.000000>>> 09.06.2017 17:41:07 - After WillPresentNotification -> PendingNotificationRequests: 0


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>