Amazon SNS push notifications not handled when Xamarin.iOS app not running - xamarin.ios

I am developing an iOS app in Xamarin, and it needs to receive push notifications from our existing back office through Amazon SNS.
For the time being I am using the amazon web page to send test notifications through to APNS_SANDBOX.
I have written my code and created the certificates for the iOS app and everything works fine when the app is running. However when the app is in the background or not loaded at all, no notifications are received by the iOS device.
In the project options in Xamarin Studio I have enabled the following in background modes
Enabled Background Modes, Background fetch, Remote notifications.
In General Settings for the iOS device, Background App Refresh is enabled globally and for the app.
I think I must have missed something very basic from either the configuration or in the apple certificates, but I can't work out what.

I have managed to find a solution after reading various solutions from various iOS/Objective C questions. It was this particular question that set me in the right direction.
There was a problem with my code to subscribe to push notifications when running on iOS 8.0
My original code:
public static void Subscribe()
{
if (UIDevice.CurrentDevice.SystemVersion [0] >= '8')
{
UIApplication.SharedApplication.RegisterForRemoteNotifications()
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
}
}
My new code:
public static void Subscribe()
{
if (UIDevice.CurrentDevice.SystemVersion [0] >= '8')
{
UIUserNotificationType types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert;
UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes (types, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
}
}
This change now allows notifications to be received correctly when the app is in the background or not running.

Related

Clear exo player notification when app stops from recent apps in android studio

I am developing an app for radio streaming using exoplayer library. So, I want to clear(delete) exoplayer notification bar (with play/stop buttons) when I close the app(from recent apps e.t.c). Now, I close the app but the notification bar still appears and doesn't close with swipe left/right.
I tried the following source code but it doesn't work (i tried to run the lines of the source code separately from the methods but I can't solve the problem).
public class RadioService extends Service {
......
.......
......
public void onTaskRemoved(Intent rootIntent) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID);
stopForeground(true);
notificationManager.cancelAll();
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
I declared it in AndroidManifest.xml too.
Any help please?
Check this answer How to kill a foreground service along with the application and an example
https://androidwave.com/foreground-service-android-example/
at android 8 and above you have to use foreground service.
Add a small delay after the clear of notification and before killing the app or create a function that will inform you the notification is cleared

Azure Notification Hub in UWP

I toast notification by Azure Notification hub to UWP Application from ASP.NET website but when I click in notification it opens app and suspend in splash screen then it close immediately , how I fix this problem ??
I use this tutorial :
https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification
when I click in notification it opens app and suspend in splash screen then it close immediately
Frequently, OnLaunched is invoked when UWP application is launched and this method is initial created. But if you want to launch the app by toast which ActivationKind is ToastNotification, you may need to handle the activated event by overriding the OnActivated event handle. For example:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.ToastNotification)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
// TODO: Handle URI activation
// The received URI is eventArgs.Uri.AbsoluteUri
var rootFrame = CreateRootFrame();
rootFrame.Navigate(typeof(MainPage));
Window.Current.Activate();
}
}
More details please reference Handle URI activation and the Notification official sample.

InterstitialAd not working with Windows Phone 10 (UWP - Windows 10)

I cant' get InterstitialAd to work in my UWP app when running on the Windows Phone Emulator (Note that I haven't tried it yet on actual real phone)
It works as expected when I run my UWP app in the Simulator or Local Machine as a Windows Store app.
Any ideas?
Thanks.
UPDATE - 1
Here is the code I use to display the InterstitialAd. In my MainPage.xaml.cs I have the following code:
public sealed partial class MainPage : Page
{
private InterstitialAd interstitialAd;
public MainPage()
{
this.InitializeComponent();
// Instantiate the interstitial video ad
interstitialAd = new InterstitialAd();
// Attach event handlers
interstitialAd.ErrorOccurred += OnAdError;
interstitialAd.AdReady += OnAdReady;
interstitialAd.Cancelled += OnAdCancelled;
interstitialAd.Completed += OnAdCompleted;
}
}
// This is an event handler for the interstitial ad. It is
// triggered when the interstitial ad is ready to play.
private void OnAdReady(object sender, object e)
{
// The ad is ready to show; show it.
interstitialAd.Show();
}
// This is an event handler for the interstitial ad. It is
// triggered when the interstitial ad is cancelled.
private void OnAdCancelled(object sender, object e)
{
}
// This is an event handler for the interstitial ad. It is
// triggered when the interstitial ad has completed playback.
private void OnAdCompleted(object sender, object e)
{
}
// This is an error handler for the interstitial ad.
private void OnAdError(object sender, AdErrorEventArgs e)
{
}
I've literarily taken this code from their UWP Store sample except that instead of launching this from a button, I'm launching it when my page is loaded:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
// Request an ad. When the ad is ready to show,
// the AdReady event will fire.
// The application id and ad unit id are passed in here.
// The application id and ad unit id can be obtained from Dev
// Center.
// See "Monetize with Ads" at https://msdn.microsoft.com/
// en-us/library/windows/apps/mt170658.aspx
#if DEBUG
interstitialAd.RequestAd(AdType.Video,
"d25517cb-12d4-4699-8bdc-52040c712cab", "11389925");
#else
interstitialAd.RequestAd(AdType.Video,
"d25517cb-12d4-4699-8bdc-52040c712cab", "11389925");
#endif
}
I've left both ApplicationId and UnitAdId as the test values for the time being as I haven't released it yet, well, not with ads anyway.
Thanks
UPDATE - 2:
I've added some debugging logs in the various events and this is what I get:
Page_Loaded: 00:00:00.0001590
Page_Loaded - RequestAd: 00:00:00.0091840
OnAdReady - Elasped Time: 00:01:04.6923865
OnAdError: NetworkConnectionFailure : Media timeout occurred
during playback. - Elasped
Time: 00:00:08.1955928
It takes over 1 minute for the OnAdReady to be triggered which is really odd and then I get an OnAdError 8 seconds later so from these logs you would assume that there is a network problem but my data is being loaded correctly which is all pulled from a web service, so there is definitely a connection. Also my AdMediator displays Ads as expected (well, kind of! That's another story!).
I will try to transfer it directly to my phone tomorrow and see if it makes any differences and I plug in my laptop to an Ethernet port instead of using the wireless but my wireless is pretty decent, so I'm not sure why I'm getting network connectivity errors.
The above code is correct and does actually work with UWP on Windows Phone but it doesn't work great in the emulator as:
it intermittently generates network connectivity errors when they are none.
it can take over a minute to display the advert but it eventually will.
I've just uploaded my app to the store and downloaded it to my phone and it works as expected. It displays the advert immediately or within a couple of seconds or so.
I would recommend you do not waste too much time trying it via the phone emulator as it appears to be the main problem. Implement your code and then test it directly on your device or download it from the store assuming you haven't released your app yet.
UPDATE:
I just thought I'd update my answer as I was just reading about the do's and dont's with interstitial adverts from UI and User Experience Guidelines and the way I'm actually doing it, is one of points in the "avoid" list.
It recommends that you fetch the advert 30-60 seconds ahead of time and you don't display it when the app starts, which is what I'm doing, so I guess I will change the logic in that regards.

How do I connect Windows Phone 8.1 (Silverlight) to Nav 2009 R2 Web Services?

I've been a .Net developer for some years now and quite a bit of experience connecting my ASP.Net and winform applications to web services. Recently I started working with Navision, specifically 2009 R2.
I have created a number of applications for ASP.Net and Winforms that connect to Navision's web services successfully and was recently asked to look into developing a Windows Phone 8.1 application to replace an existing application that runs on tablet pcs, and thats where its all gone horribly wrong...
After reading up and a few cases of trial and error, it seems that when I open Visual Studio 2013 and create a Windows Phone 8.1 Runtime application, it has no way of adding a Web Reference or Service Reference to the Navision Web Services.
I started again with w Windows Phone 8.1 (Silverlight) application and at least I get the option to add a Service Reference.
I added the Service Reference that point to my Web Service but the web service requires that the user set a set of credentials (a Windows Domain user account) and at no point was I prompted to enter these. Ok, maybe they're set at runtime...
After following a post in an Microsoft blog that covers connecting to Navision web services using Silverlight 3, I had most of my code in place, but there was still no option to enter any user credentials. It seems that there's a difference between regular Silverlight and the version used in the Windows Phone development environment.
The following code is a method taken from my test application...
protected void ConnectToNav()
{
try
{
string serviceURL = "http://192.168.0.50:7047/DynamicsNAV/WS/TestCompany/Page/svcUser";
svcUser_PortClient userService = new svcUser_PortClient("svcUser_Port", new EndpointAddress(serviceURL));
userService.ClientCredentials.UserName.UserName = #"Domain\User";
userService.ClientCredentials.UserName.Password = #"Password";
userService.ReadMultipleCompleted += delegate(object sender, ReadMultipleCompletedEventArgs e)
{
display(e.Error.Message);
if (e.Result.Length > 0)
{
display("10 Users:");
for (int i = 0; i < e.Result.Length; i++)
{
display(e.Result[i].ToString());
}
}
};
List<svcUser_Filter> filters = new List<svcUser_Filter>();
userService.ReadMultipleAsync(filters.ToArray(), null, 10);
}
catch (Exception ex)
{
display(ex.Message);
}
}
public void display(string s)
{
this.OutputTextBox.Text += s + Environment.NewLine;
}
and this is the error that gets returned...
The remote Server returned an unexpected response: (401) Unauthorized. In Silverlight, a 404 response code may be reported even when the service sends a difference error code.
I'd love to hear from anyone that has either got this working, or know of any good reference for this and how to get it working.
I would recommend using Windows Phone Runtime for easy of migration going forward. What you could do is use ASP.NET Web API and consume the NAV web service in that, while you then call the Web API from your Windows Phone app. Would this work for you?
Here's a good resource on ASP.NET Web API.

how to integrate vserv ads sdk to windows phone 8 application

I have developed a windows phone 8 application. Now I want to display vserv ads to my application.
I have added the sdk to my application and also applied code to show ads:
public MainPage()
{
InitializeComponent();
VservAdControl VMB = VservAdControl.Instance;
VMB.DisplayAd(zoneID, LayoutRoot);
VMB.VservAdClosed += new EventHandler(VACCallback_OnVservAdClosing);
VMB.VservAdNoFill += new EventHandler(VACCallback_OnVservAdNoFill);
}
void VACCallback_OnVservAdClosing(object sender, EventArgs e)
{
MessageBox.Show("Ad close by user.");
}
void VACCallback_OnVservAdNoFill(object sender, EventArgs e)
{
if (adGrid != null)
adGrid.Visibility = Visibility.Collapsed;
}
But after closing the ad the application page goes blank, all content, application bar automatically goes blank. After using back arrow that quits my application, i try to relaunch my application but application stuck at the splash screen on the emulator.
Wrap ad control inside a grid. Ad control could have manipulated the grid
I encountered the same issue and they have updated their SDK several times. If you provide the stacktrace they will provide you a fix.
Apart from what you have seen, there are other issues with the SDK. I integrated it few weeks back. So issues could have been resolved after that.
Memory leak. Click the ad or navigate back and forth - you will see the memory ever growing. This is because of events not being detached (withing SDK). I was consistently able to see my app crash in 512 MB emulator when banner ad is loaded (after 4 - 5 times). They could have used weak listeners. You might need to tweak a lot to overcome this issue (In multi page app)
RequestAdCallback throws null pointer exception sometimes crashing
the app. When people use app, they will navigate fast - forcing the
webbrowser to unload. All callbacks should be null pointer exception
free. Make sure that you handle unhandled exception globally otherwise app will not pass certification
It reads WMAppManifest.xml as text not as XML. So I had App element commented in first line before the actual one. It picked title from commented XML element
Application bar is manipulated in many events in the SDK. So you have to make sure that app bar is built dynamically. Test all navigation paths.
SDK assumes user will click the left arrow button which fires ad closing event. Try pressing phone back button instead. The app bar still disappears
SDK documentation requests app to demand ID_CAP_REMOVABLE_STORAGE capability. I don't see a reason to request this capability but I didn't add this
I have emailed them all these details. May be their latest SDK could have resolved these issues. But please do thorough testing after integration.
Add a function to load applicationbar using code
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = false;
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton button1 = new ApplicationBarIconButton();
button1.IconUri = new Uri("/Images/sms.png", UriKind.Relative);
button1.Text = "sms";
ApplicationBar.Buttons.Add(button1);
}
and then in VACCallback_OnVservAdClosing event handler call the function
BuildLocalizedApplicationBar()
Please check the following link,
where to add application id and adUnitiD while integrating vserv ads sdk to windows phone 8 application, which is related to your question.

Resources