Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to get push/remote notification when app in foreground in xamarin.ios native app?

sumitmishrasumitmishra Member ✭✭✭
edited July 2018 in Xamarin.iOS

Hi,
We have implemented APNS push notification in Xamarin.ios native app and its work fine when app in background or kill state.
We have unable to get notification in foreground state. How to solve this?
Can any one suggest how to implement c# code for this

userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification

Xamarin.ios native app . Please help/guide.

Tagged:

Posts

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    Hi,

    [Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")] public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { if (UIApplication.SharedApplication.ApplicationState.Equals(UIApplicationState.Active)) { BeginInvokeOnMainThread(async () => { UIAlertController okayAlertController = UIAlertController.Create(“Alert Header”, message, UIAlertControllerStyle.Alert); okayAlertController.AddAction(UIAlertAction.Create(“OK”, UIAlertActionStyle.Default, (obj) => { // Your logic })); NavigationController.PresentViewController(okayAlertController, true, null); }); } }

  • Imran_619Imran_619 Member ✭✭✭

    You can call completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);

    in userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification

    Default is UNNotificationPresentationOptions.None when on foreground which usually is normal because you don't want to show a push notif in when the app is in foreground unless your app requires it.

    For more information on mobile app development you can visit ArStudioz

  • sumitmishrasumitmishra Member ✭✭✭

    Hello Everyone,
    Please check my code. This is working when apps in background or kill state. In my code else part is working for APNS push notification using apple apns certificate but I want same push notification when app in foreground for upcoming message in another group or chat. I don't want a popup notification. I want a notification in notification center like whatsapp. Please help.

    //FinishedLaunching event code
         if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                    {
                        var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                                           UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                                           new NSSet());
    
                        UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                        UIApplication.SharedApplication.RegisterForRemoteNotifications();
                    }
                    else
                    {
                        UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                        UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
                    }
    
    
      public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
            {
                if (application.ApplicationState == UIApplicationState.Active)
                {
                   UILocalNotification notification = new UILocalNotification();
                    NSDate.FromTimeIntervalSinceNow(15);
                    //notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
                    notification.AlertAction = "View Alert";
                    notification.AlertBody = "Your 15 second alert has fired!";
                    notification.ApplicationIconBadgeNumber = 1;
                    notification.SoundName = UILocalNotification.DefaultSoundName;
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    
    
    
                }
                else
                {
                    var GroupID = userInfo["GroupID"].ToString().Replace("{", "").Replace("}", "");
                    var GroupName = userInfo["GroupName"].ToString().Replace("{", "").Replace("}", "");
    
                    ChatDetaiils.groupID = Convert.ToInt32(GroupID);
                    ChatDetaiils.groupName = GroupName.ToString();
    
                    var ChatRoomsViewController = GetViewController(MainStoryboard, "ChatRoomsViewController") as 
               ChatRoomsViewController;
                    SetRootViewController(ChatRoomsViewController, false);
                }
    
            }
    
Sign In or Register to comment.