我一直在嘗試幾個小時來爲Android啓用推送通知,因此我可以在Amazon SNS中使用它。試圖遵循文檔中描述的代碼:http://docs.aws.amazon.com/mobile/sdkforxamarin/developerguide/getting-started-sns-android.html如何在Android(Xamarin)上爲AWS SNS啓用推送通知
好像我做錯了什麼,因爲我創建的「意圖」有「行動」設置爲null,這會導致OnHandleIntent中的異常。有沒有人有這方面的經驗?我對Android很新,所以我對「意圖」的理解很有限。
這裏是在MainActivity
[Activity (Label = "awst", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
var intent = new Intent (this, typeof (GCMIntentService));
};
}
}
[BroadcastReceiver(Permission = "com.google.android.c2dm.permission.SEND")]
[IntentFilter(new string[] {
"com.google.android.c2dm.intent.RECEIVE"
}, Categories = new string[] {
"com.companyname.awst" /* change to match your package */
})]
[IntentFilter(new string[] {
"com.google.android.c2dm.intent.REGISTRATION"
}, Categories = new string[] {
"com.companyname.awst" /* change to match your package */
})]
[IntentFilter(new string[] {
"com.google.android.gcm.intent.RETRY"
}, Categories = new string[] {
"com.companyname.awst" /* 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);
}
}
}
和意圖服務
namespace awst.Droid
{
[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;
// !!!!!!
// this is where the code fails with action beeing null
// !!!!!!
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) {
Globals config = Globals.Instance;
string registrationId = intent.GetStringExtra("registration_id");
string error = intent.GetStringExtra("error");
string unregistration = intent.GetStringExtra("unregistered");
if (string.IsNullOrEmpty(error)) {
config.snsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest {
Token = registrationId,
PlatformApplicationArn = config.AWS_PlaformARN /* 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("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)
//todo
.SetSmallIcon(Resource.Mipmap.Icon)
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
// Get the notification manager:
NotificationManager notificationManager = this.GetSystemService(Context.NotificationService) as NotificationManager;
notificationManager.Notify(1001, builder.Build());
}
}
}
所以問題:
如何註冊的設備,所以我可以再從SNS發送推?有什麼不同的方式我應該考慮? 有沒有其他的步驟我必須採取這個工作?我沒有將證書上傳到AWS,但我是否需要在應用代碼中配置任何權限?
非常感謝!
克里斯
太感謝你了 - 這肯定是丟失。我已經下載了示例,並試圖:我認爲在Constants.GoogleConsoleProjectId中,我實際上需要的只是項目編號而不是編號。是對的嗎?現在我已經能夠運行該示例,並且我確實獲得了在SNS上註冊的新端點,但是當我使用SNS控制檯發送推送時,沒有任何反應。還有什麼我需要做的?我是否需要在應用程序中訂閱某個主題? anyhwere我可以檢查什麼是錯的? ....謝謝 – chrisb
解決 - 只是在GCM和更新的SNS重新生成項目,它的工作原理! – chrisb
男人你救了我無數個小時! aws文檔很爛,所以過時了..我一直在尋找幾個小時在他們的文檔沒有運氣! –