cheran
cheran

Reputation: 483

Push Notification using Amazon SNS – Device id

Developing the mobile app using the Xamarin Forms. For push notification we are using Amazon Simple Notification Service(SNS).

Xamarin.Andriod : 1. While installing the app we have used the below code snippet to register the device id into the Amazon SNS in OnCreate method of MainActivity. It works fine

using (Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"))
                {
                    string senders = AmazonUtils.GoogleConsoleProjectId;
                    intent.SetPackage("com.google.android.gsf");
                    intent.PutExtra("app", PendingIntent.GetBroadcast(this, 0, new Intent(), 0));
                    intent.PutExtra("sender", senders);
                    this.StartService(intent);
                }
  1. Every time when app opens checking the corresponding device id is registered in the Amazon SNS. Due to this app takes additional 4 secs to check this process and after that page is loading.

  2. Do we need to check the device is register or not for every time when the app opens ?. Is this standard for the push notification ?.

Regards, Cheran

Upvotes: 3

Views: 482

Answers (1)

Wilson Vargas
Wilson Vargas

Reputation: 2899

Install Xam.Plugins.Settings.

It will add a helper class called Settings

In this class you should add:

 private const string IsRegisteredKey = "registered_key";
 private static readonly bool IsRegisteredDefault = false;

 //Then adding this property
 public static bool IsRegistered
 {
     get
     {
         return AppSettings.GetValueOrDefault(IsRegisteredKey, IsRegisteredDefault);
     }
     set
     {
         AppSettings.AddOrUpdateValue(IsRegisteredKey, value);
     }
 }

Then in your code call this property, like this:

using YourProjectNameSpace.Droid.Helper

....

if(!Settings.IsRegistered)
{
    using (Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"))
    {
         string senders = AmazonUtils.GoogleConsoleProjectId;
         intent.SetPackage("com.google.android.gsf");
         intent.PutExtra("app", PendingIntent.GetBroadcast(this, 0, new Intent(), 0));
         intent.PutExtra("sender", senders);
         this.StartService(intent);
    }
    Settings.IsRegistered = true;
}

Upvotes: 1

Related Questions