3

使用Xamarin Forms開發移動應用程序。對於推送通知,我們使用亞馬遜簡單通知服務(SNS)。使用Amazon SNS的推送通知 - 設備ID

Xamarin.Andriod: 1.在安裝該應用程序,我們使用了下面的代碼段要註冊的設備ID到亞馬遜SNS中MainActivity的onCreate方法。它工作正常

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); 
       } 
  • 當應用打開檢查相應的設備ID在亞馬遜SNS中註冊每一次。由於此應用程序需要額外的4秒來檢查此過程,並在該頁面加載之後。

  • 我們是否需要在應用程序每次打開時檢查設備是否註冊?這是推送通知的標準嗎?

  • 問候, 基蘭

    回答

    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; 
    } 
    
    相關問題