コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"cognito-identity:*"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"sns:CreatePlatformEndpoint",
"sns:Subscribe",
"sns:SetEndpointAttributes"
],
"Resource": [
"arn:aws:sns:us-west-9:999999999999:*"
]
}
]
}
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>production</string> <!--RELEASE MODE-->
<!--<string>development</string> DEBUG MODE-->
</dict>
</plist>
using Amazon;
using Amazon.Util;
using Amazon.CognitoIdentity;
using Amazon.SimpleNotificationService;
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
//通知の許可を求める
DependencyService.Get<INotificationService>().Regist();
//AWS
this.SetAmazonSnsSettings();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
#region "AWS"
private AmazonSimpleNotificationServiceClient _snsClient = null;
/// <summary>
/// AWS認証
/// </summary>
private void SetAmazonSnsSettings()
{
//AWSログの設定
var loggingConfig = AWSConfigs.LoggingConfig;
loggingConfig.LogMetrics = true;
loggingConfig.LogResponses = ResponseLoggingOption.Always;
loggingConfig.LogMetricsFormat = LogMetricsFormatOption.JSON;
loggingConfig.LogTo = LoggingOptions.SystemDiagnostics;
//AWSの認証
AWSConfigs.AWSRegion = "us-west-9";
AWSConfigs.CorrectForClockSkew = true;
var offset = AWSConfigs.ClockOffset;
CognitoAWSCredentials credentials = new CognitoAWSCredentials(
"us-west-9:XXXXXXX-9999-9999-XXXX-XXXXXXXXXX", //Cognito identity pool ID
RegionEndpoint.USWest9 // Region
);
_snsClient = new AmazonSimpleNotificationServiceClient(credentials, RegionEndpoint.USWest2);
}
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
{
// without this RegisteredForRemoteNotifications doesn't fire on iOS 8!
application.RegisterForRemoteNotifications();
}
/// <summary>
/// AWSへのEndPoint登録処理
/// </summary>
/// <param name="application"></param>
/// <param name="deviceToken"></param>
public override async void RegisteredForRemoteNotifications(
UIApplication application, NSData deviceToken)
{
// Get current device token
var DeviceToken = deviceToken.Description;
if (!String.IsNullOrWhiteSpace(DeviceToken))
{
DeviceToken = DeviceToken.Trim('<').Trim('>').Replace(" ", "");
//register with SNS to create an endpoint ARN
var response = await _snsClient.CreatePlatformEndpointAsync(
new CreatePlatformEndpointRequest
{
Token = DeviceToken,
PlatformApplicationArn = "arn:aws:sns:us-west-9:9999999999:app/APNS_SANDBOX/AppName" /* insert your platform application ARN here */
});
}
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
// Has the token changed?
if (String.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
//TODO: Put your own logic here to notify your server that the device token has changed/been created!
}
// Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}
/// <summary>
/// プッシュ通知の受信処理
/// </summary>
/// <param name="application"></param>
/// <param name="userInfo"></param>
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
//baseは実行しない(エラーとなるため)
//base.ReceivedRemoteNotification(application, userInfo);
if (application.ApplicationState == UIApplicationState.Active)
{
//アプリがアクティブの場合
}
else if (application.ApplicationState == UIApplicationState.Inactive)
{
//アプリがアクティブでない場合
}
//ローカル通知で表示する場合(無くてもプッシュ通知表示されます)
NotificationService ns = new NotificationService();
ns.On("RemoteNotifications title",
"RemoteNotifications subtitle",
"RemoteNotifications body",
null);
}
/// <summary>
/// プッシュ通知の登録失敗
/// </summary>
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show();
}
#endregion
}
public void Regist()
{
//ローカル通知の許可を求める
・
・省略
・
UIApplication.SharedApplication.InvokeOnMainThread(() => {
//プッシュ通知
UIApplication.SharedApplication.RegisterForRemoteNotifications();
});
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。