使用Amazon SNS进行推送通知 - 设备ID

3

使用Xamarin Forms开发移动应用程序。对于推送通知,我们使用Amazon Simple Notification Service(SNS)。

Xamarin.Andriod: 1. 在安装应用程序时,我们在MainActivity的OnCreate方法中使用以下代码片段将设备ID注册到Amazon SNS中。它能正常工作。

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. 每次应用程序打开时,都要检查相应的设备 ID 是否已在 Amazon SNS 中注册。由于这个过程需要额外的 4 秒来检查,因此页面加载较慢。
  2. 我们需要每次打开应用程序时都检查设备是否已注册吗?这是推送通知的标准吗?

谢谢, Cheran

1个回答

1

安装Xam.Plugins.Settings

它会添加一个帮助类叫做Settings

在这个类中,您应该添加:

 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);
     }
 }

然后在你的代码中调用这个属性,像这样:

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接