Xamarin 的AWS行動 SDK 現在已包含在AWS SDK for .NET. 本指南參考 Xamarin 行動 SDK 的封存版本。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 SNS 接收推送通知
本教程介紹瞭如何使用 Amazon Simple Notification Service (SNS) 和適用於 .NET 和 Xamarin 的 AWS 移動軟件開發工具包向 Xamarin Android 應用程序發送推送通知。
專案設定
先決條件
您必須完成設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK,然後再開始本教程。
設定 SNS 的許可
按照步驟 2設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK將下面提到的策略附加到應用程序的角色。這將為您的應用程序授予訪問 SNS 的適當權限:
-
前往IAM 主控台
並選取您想要設定的 IAM 角色。 -
按一下連接政策,請選取卓越亞馬遜 SNFullAccess策略,然後單擊連接政策。
警告
使用卓越亞馬遜 SNFullAccess不建議在生產環境中使用。我們在這裏使用它來幫助您快速啟動並執行。如需為 IAM 角色指定許可的詳細資訊,請參IAM 角色許可概觀。
在谷歌雲端啟用推送通知
首先,添加一個新的谷歌 API 項目:
-
按一下建立專案。
-
在 中新專案框中,輸入項目名稱,記下項目 ID(稍後需要),然後單擊建立。
接下來,啟用適用於您的專案的 Google Cloud Message (GCM) 服務:
-
在 中Google 開發人員主控台
,您的新專案應該已選取起來。如果沒有,請在頁面頂端的下拉式選取該選項。 -
選擇API 和驗證位於頁面左側的側欄。
-
在搜尋方塊中,輸入適用於 Android 的 Google Cloud Message (適用於 Android 的 Google Message),然後適用於安卓的谷歌雲消息鏈接。
-
按一下啟用 API。
最後,獲取 API 密鑰:
-
在谷歌開發人員控制台中,選擇API 和驗證 >登入資料。
-
Unblic公用 API 訪問,請單擊建立新的金鑰。
-
在 中建立新的金鑰對話框中,單擊服務器密鑰。
-
在生成的對話框中,單擊建立並複製顯示的 API 密鑰。您稍後將使用此 API 密鑰執行身份驗證。
使用項目 ID 在 SNS 控制台中創建平台 ARN
-
前往SNS 控制台
。 -
按一下應用程式在畫面左側。
-
按一下建立平台應用程式建立新 SNS 平台應用程式。
-
輸入應用程式名稱。
-
選擇Google Cloud Message (GCM)為了推播通知平台。
-
將 API 密鑰粘貼至標記為API 金鑰。
-
按一下建立平台應用程式。
-
選擇剛剛創建的平台應用程序並複製應用程序 ARN。
AddNuGetSNS 軟件包到您的項目
請按照中的説明步驟 4設定適用於 .NET 和 Xamarin 的 AWS Mobile SDK添加 Amazon Simple Notification ServiceNuGet包添加至您的專案。
建立 SNS 客户端
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, region);
註冊應用程序以獲得遠程通知
若要在 Android 上註冊遠程通知,您需要建立BroadcastReceiver,它可以接收谷歌雲消息。在提示時更改下面的軟件包名稱:
[BroadcastReceiver(Permission = "com.google.android.c2dm.permission.SEND")] [IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "com.amazonaws.sns" /* change to match your package */ })] [IntentFilter(new string[] { "com.google.android.c2dm.intent.REGISTRATION" }, Categories = new string[] { "com.amazonaws.sns" /* change to match your package */ })] [IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, Categories = new string[] { "com.amazonaws.sns" /* change to match your package */ })] public class GCMBroadcastReceiver: BroadcastReceiver { const string TAG = "PushHandlerBroadcastReceiver"; public override void OnReceive(Context context, Intent intent) { GCMIntentService.RunIntentInService(context, intent); SetResult(Result.Ok, null, null); } } [BroadcastReceiver] [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })] public class GCMBootReceiver: BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { GCMIntentService.RunIntentInService(context, intent); SetResult(Result.Ok, null, null); } }
以下是接收來自BroadcastReceiver並在設備的通知欄上顯示通知:
[Service] public class GCMIntentService: IntentService { static PowerManager.WakeLock sWakeLock; static object LOCK = new object(); public static void RunIntentInService(Context context, Intent intent) { lock(LOCK) { if (sWakeLock == null) { // This is called from BroadcastReceiver, there is no init. var pm = PowerManager.FromContext(context); sWakeLock = pm.NewWakeLock( WakeLockFlags.Partial, "My WakeLock Tag"); } } sWakeLock.Acquire(); intent.SetClass(context, typeof(GCMIntentService)); context.StartService(intent); } protected override void OnHandleIntent(Intent intent) { try { Context context = this.ApplicationContext; string action = intent.Action; if (action.Equals("com.google.android.c2dm.intent.REGISTRATION")) { HandleRegistration(intent); } else if (action.Equals("com.google.android.c2dm.intent.RECEIVE")) { HandleMessage(intent); } } finally { lock(LOCK) { //Sanity check for null as this is a public method if (sWakeLock != null) sWakeLock.Release(); } } } private void HandleRegistration(Intent intent) { string registrationId = intent.GetStringExtra("registration_id"); string error = intent.GetStringExtra("error"); string unregistration = intent.GetStringExtra("unregistered"); if (string.IsNullOrEmpty(error)) { var response = await SnsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest { Token = registrationId, PlatformApplicationArn = "YourPlatformArn" /* insert your platform application ARN here */ }); } } private void HandleMessage(Intent intent) { string message = string.Empty; Bundle extras = intent.Extras; if (!string.IsNullOrEmpty(extras.GetString("message"))) { message = extras.GetString("message"); } else { message = extras.GetString("default"); } Log.Info("Messages", "message received = " + message); ShowNotification(this, "SNS Push", message); //show the message } public void ShowNotification(string contentTitle, string contentText) { // Intent Notification.Builder builder = new Notification.Builder(this) .SetContentTitle(contentTitle) .SetContentText(contentText) .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate) .SetSmallIcon(Resource.Drawable.Icon) .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)); // Get the notification manager: NotificationManager notificationManager = this.GetSystemService(Context.NotificationService) as NotificationManager; notificationManager.Notify(1001, builder.Build()); } }
從 SNS 控制台向終端節點發送消息
-
選擇您的平台應用程序,選擇終端節點,然後單擊發佈至終端。
-
在文字方塊中輸入文字信息,然後單擊發佈訊息發佈訊息。