Saturday 23 January 2021

Receive Push Notifications using SNS (Xamarin Android)

What Nuget Required ? 

Plugin.FirebasePushNotification (I have used 3.3.10) you can use latest one.


First we create a Unique Device ID using below


#region Reterive and Store DeviceID

// Reterive and Store DeviceID

string deviceId = string.Empty;

if (string.IsNullOrEmpty(Helper.UserInfo?.UniqueDeviceID))

{

deviceId = DependencyService.Get<IDeviceId>().GetDeviceId();

Helper.UserInfo.UniqueDeviceID = deviceId;

}

#endregion



in Android Project


[assembly: Xamarin.Forms.Dependency(typeof(TestSNSFirebase.iOS.Services.DeviceId_Android))]

namespace TestSNSFirebase.iOS.Services

{

public class DeviceId_Android : IDeviceId

{

public string GetDeviceId()

{

return Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);

}

}

}


you can use this for testing here



You have to Create Three Account online [ There Android Project need to created inside the 


1. Google (https://console.developers.google.com/apis/) [Firebase Push Notification][ You have to Create a Project here as in below screen.






Then you have to create API Key here which need to required in 


2. Amazon SNS account


3. Firebase Account [ With Project ]


you have to download the json file from here

Get config file for your Android app

  1. Go to your   Project settings in the Firebase console.

  2. In the Your apps card, select the package name of the app for which you need a config file.

  3. Click   google-services.json.

  4. Move your config file into the module (app-level) directory of your app.
    Make sure that you only have this most recent downloaded config file in your app.







Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.



In Android Main Activity

protected override void OnNewIntent(Intent intent)

{

base.OnNewIntent(intent);

FirebasePushNotificationManager.ProcessIntent(this, intent);

}


and Inside the App.xaml.cs  CrossFirebasePushNotification)


private void PushNotfication()

{

// Handle when your app starts

CrossFirebasePushNotification.Current.Subscribe("general");

CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>

{

System.Diagnostics.Debug.WriteLine($"TOKEN REC: {p.Token}");

};

System.Diagnostics.Debug.WriteLine($"TOKEN: {CrossFirebasePushNotification.Current.Token}");

CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>

{

try

{

System.Diagnostics.Debug.WriteLine("Received");

if (p.Data.ContainsKey("body"))

{

Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>

{

Alert.ShowAlert($"{p.Data["body"]}");

});

}

CrossFirebasePushNotification.Current.UnsubscribeAll();

}

catch (Exception ex)

{

System.Diagnostics.Debug.WriteLine(ex.Message);

}

};

CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>

{

//System.Diagnostics.Debug.WriteLine(p.Identifier);

System.Diagnostics.Debug.WriteLine("Opened");

foreach (var data in p.Data)

{

System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");

}

if (!string.IsNullOrEmpty(p.Identifier))

{

Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>

{

Alert.ShowAlert(p.Identifier);

});

}

else if (p.Data.ContainsKey("aps.alert.title"))

{

Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>

{

Alert.ShowAlert($"{p.Data["aps.alert.title"]}");

});

}

};

CrossFirebasePushNotification.Current.OnNotificationAction += (s, p) =>

{

System.Diagnostics.Debug.WriteLine("Action");

if (!string.IsNullOrEmpty(p.Identifier))

{

System.Diagnostics.Debug.WriteLine($"ActionId: {p.Identifier}");

foreach (var data in p.Data)

{

System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");

}

}

};

CrossFirebasePushNotification.Current.OnNotificationDeleted += (s, p) =>

{

System.Diagnostics.Debug.WriteLine("Dismissed");

};

}


Steps is present in below link with more details , I just given some basic idea , Note that Project ID , Key , secret key is important to keep noted. 


https://docs.aws.amazon.com/mobile/sdkforxamarin/developerguide/getting-started-sns-android.html

https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/GettingStarted.md


2 comments:

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2