-
StackOverflow 文件
-
Xamarin.Forms 教程
-
推送通知
-
iOS 示例
- 你將需要一個開發裝置
- 轉到 Apple 開發人員帳戶並建立啟用推送通知的配置檔案
- 你將需要某種方式來通知你的手機(AWS,Azure ..等) 我們將在此處使用 AWS
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
//after typical Xamarin.Forms Init Stuff
//variable to set-up the style of notifications you want, iOS supports 3 types
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound,
null );
//both of these methods are in iOS, we have to override them and set them up
//to allow push notifications
app.RegisterUserNotificationSettings(pushSettings); //pass the supported push notifications settings to register app in settings page
}
public override async void RegisteredForRemoteNotifications(UIApplication application, NSData token)
{
AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient("your AWS credentials here");
// This contains the registered push notification token stored on the phone.
var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", "");
if (!string.IsNullOrEmpty(deviceToken))
{
//register with SNS to create an endpoint ARN, this means AWS can message your phone
var response = await snsClient.CreatePlatformEndpointAsync(
new CreatePlatformEndpointRequest
{
Token = deviceToken,
PlatformApplicationArn = "yourARNwouldgohere" /* insert your platform application ARN here */
});
var endpoint = response.EndpointArn;
//AWS lets you create topics, so use subscribe your app to a topic, so you can easily send out one push notification to all of your users
var subscribeResponse = await snsClient.SubscribeAsync(new SubscribeRequest
{
TopicArn = "YourTopicARN here",
Endpoint = endpoint,
Protocol = "application"
});
}
}